From: Jan "Yenya" Kasprzak Date: Sun, 16 Jan 2022 18:31:51 +0000 (+0100) Subject: AoC 2017 begins X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=d8bd90fc70b64ebe062eeb624c46abab2f811a64;p=aoc.git AoC 2017 begins --- diff --git a/2017/01.pl b/2017/01.pl new file mode 100755 index 0000000..77c9077 --- /dev/null +++ b/2017/01.pl @@ -0,0 +1,11 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp (my $data = <>); + +$data =~s /\A(.)(.*)\z/\1\2\1/; +my $sum = 0; +$sum += $1 while $data =~ /(.)(?=\1)/g; +say $sum; diff --git a/2017/02.pl b/2017/02.pl new file mode 100755 index 0000000..6726b6b --- /dev/null +++ b/2017/02.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp (my $data = <>); + +my $l = length $data; +$l /= 2; +$data .= substr($data, 0, $l); +$l--; +my $sum = 0; +$sum += $1 while $data =~ /(.)(?=.{$l}\1)/g; +say $sum; diff --git a/2017/03.pl b/2017/03.pl new file mode 100755 index 0000000..df14649 --- /dev/null +++ b/2017/03.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +use List::Util qw(min max); + +my $res = 0; +while (<>) { + my @a = split /\s+/; + my $m1 = min @a; + my $m2 = max @a; + $res += $m2-$m1; +} + +say $res; + diff --git a/2017/04.pl b/2017/04.pl new file mode 100755 index 0000000..2cd7f4f --- /dev/null +++ b/2017/04.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +use List::Util qw(min max); + +my $res = 0; +while (<>) { + my @a = split /\s+/; + for my $i (0 .. $#a) { + for my $j (0 .. $#a) { + next if $i == $j; + if (int ($a[$i]/$a[$j])*$a[$j] == $a[$i]) { + $res += $a[$i]/$a[$j]; + } + } } +} + +say $res; + diff --git a/2017/05.pl b/2017/05.pl new file mode 100755 index 0000000..f37fa53 --- /dev/null +++ b/2017/05.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my $in = 368078; + +my $diam = 0; +$diam++ while $in > (2*$diam+1)*(2*$diam+1); +my $dist = 2*$diam-1; +my $dir = -1; +my $i = (2*$diam-1)*(2*$diam-1)+1; +while ($i < $in) { + # say "\t$i $dist $dir"; + if ($dist <= $diam) { + $dir = 1; + } elsif ($dist > 2*$diam-1) { + $dir = -1; + } + $dist += $dir; + $i++; +} + +say $dist; diff --git a/2017/06.pl b/2017/06.pl new file mode 100755 index 0000000..b22f254 --- /dev/null +++ b/2017/06.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my $in = 368078; + +my ($x, $y) = (1, 0); +my $r = 1; +$; = ','; +my %map = ("0,0" => 1); +sub set { + my ($x, $y) = @_; + my $sum = 0; + for my $dx (-1 .. 1) { + for my $dy (-1 .. 1) { + $sum += $map{$x+$dx,$y+$dy} if defined + $map{$x+$dx,$y+$dy}; + } } + $map{$x,$y} = $sum; + + say "$x, $y = ", $sum; + exit 0 if $sum > $in; +} + +while (1) { + for my $i (0 .. 2*$r-1) { + set($x, $y-$i); + } + $y -= 2*$r-1; + for my $i (1 .. 2*$r) { + set($x-$i, $y); + } + $x -= 2*$r; + for my $i (1 .. 2*$r) { + set($x, $y+$i); + } + $y += 2*$r; + for my $i (1 .. 2*$r) { + set($x+$i, $y); + } + $x += 2*$r+1; + $r++; +} diff --git a/2017/07.pl b/2017/07.pl new file mode 100755 index 0000000..4950de8 --- /dev/null +++ b/2017/07.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my $valid = 0; +LINE: +while (<>) { + chomp; + my %seen; + for my $w (split /\s+/) { + next LINE if $seen{$w}++; + } + $valid++; +} + +say $valid; + diff --git a/2017/08.pl b/2017/08.pl new file mode 100755 index 0000000..4017f65 --- /dev/null +++ b/2017/08.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my $valid = 0; +LINE: +while (<>) { + chomp; + my %seen; + for my $w (split /\s+/) { + my $w1 = join('', sort split //, $w); + next LINE if $seen{$w1}++; + } + $valid++; +} + +say $valid; + diff --git a/2017/09.pl b/2017/09.pl new file mode 100755 index 0000000..1fc2ca4 --- /dev/null +++ b/2017/09.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + + +my $pc = 0; +chomp (my @offs = <>); + +my $steps = 0; +while ($pc >= 0 && $pc < @offs) { + say "$steps $pc $offs[$pc]"; + $pc += $offs[$pc]++; + $steps++; +} + +say $steps; diff --git a/2017/10.pl b/2017/10.pl new file mode 100755 index 0000000..7aad6f1 --- /dev/null +++ b/2017/10.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + + +my $pc = 0; +chomp (my @offs = <>); + +my $steps = 0; +while ($pc >= 0 && $pc < @offs) { + # say "$steps $pc $offs[$pc]"; + my $off = $offs[$pc]; + if ($off >= 3) { + $offs[$pc]--; + } else { + $offs[$pc]++; + } + $pc += $off; + $steps++; +} + +say $steps; diff --git a/2017/11.pl b/2017/11.pl new file mode 100755 index 0000000..1668038 --- /dev/null +++ b/2017/11.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp (my @banks = split /\s+/, <>); + +my %seen; +my $steps; +while (1) { + my $key = join(',', @banks); + last if $seen{$key}; + $steps++; + my ($max, $maxi); + for my $i (0 .. $#banks) { + if ($banks[$i] > $max) { + $max = $banks[$i]; + $maxi = $i; + } + } + $banks[$maxi] = 0; + while ($max--) { + $maxi = 0 if ++$maxi > $#banks; + $banks[$maxi]++; + } +} +say $steps; diff --git a/2017/12.pl b/2017/12.pl new file mode 100755 index 0000000..f9b8b0b --- /dev/null +++ b/2017/12.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp (my @banks = split /\s+/, <>); + +my %seen; +my $steps; +my $another; +while (1) { + my $key = join(',', @banks); + last if defined $another && $another eq $key; + if ($seen{$key}++ && !defined $another) { + $another = $key; + $steps = 0; + } + $steps++; + my ($max, $maxi); + for my $i (0 .. $#banks) { + if ($banks[$i] > $max) { + $max = $banks[$i]; + $maxi = $i; + } + } + $banks[$maxi] = 0; + while ($max--) { + $maxi = 0 if ++$maxi > $#banks; + $banks[$maxi]++; + } +} +say $steps; diff --git a/2017/13.pl b/2017/13.pl new file mode 100755 index 0000000..90c7c4b --- /dev/null +++ b/2017/13.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my %all; +my %below; +my %top; + +while (<>) { + my ($name, $num, $rest) = /^(\w+) \((\d+)\)(.*)/; + $all{$name} = $num; + next if !length $rest; + for my $t ($rest =~ /(\w+)/g) { + $below{$t} = $name; + $top{$t} = 1; + } +} + +for my $node (keys %all) { + say $node if !$top{$node}; +} + + + diff --git a/2017/14.pl b/2017/14.pl new file mode 100755 index 0000000..56740ee --- /dev/null +++ b/2017/14.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my %all; +my %above; +my %top; + +while (<>) { + my ($name, $num, $rest) = /^(\w+) \((\d+)\)(.*)/; + $all{$name} = $num; + next if !length $rest; + for my $t ($rest =~ /(\w+)/g) { + push @{ $above{$name} }, $t; + $top{$t} = 1; + } +} + +my $start = 'uownj'; + +sub walk { + my ($node) = @_; + my %sizes; + return $all{$node} + if !exists $above{$node}; + + my $sum; + my %nodes; + for my $ab (@{ $above{$node} }) { + my $s = walk($ab); + $sum += $s; + $sizes{$s}++; + $nodes{$s} = $ab; + } + my @sizes = sort { $sizes{$a} <=> $sizes{$b} } keys %sizes; + if (@sizes == 1) { + return $sum + $all{$node}; + } else { + say $all{$nodes{$sizes[0]}} + $sizes[1]-$sizes[0]; + exit 0; + } +} + +walk('uownj'); + + + diff --git a/2017/15.pl b/2017/15.pl new file mode 100755 index 0000000..9479114 --- /dev/null +++ b/2017/15.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + + +my %regs; +while (<>) { + chomp; + my ($reg, $op, $val, $creg, $cop, $cval) = + /^(\w+) (\S\Sc) (-?\d+) if (\w+) (\S+) (-?\d+)/; + $regs{$reg} //= 0; + $regs{$creg} //= 0; + say "$reg $op $val $creg $cop $cval"; + if ($cop eq '<' && $regs{$creg} < $cval + || $cop eq '>' && $regs{$creg} > $cval + || $cop eq '==' && $regs{$creg} == $cval + || $cop eq '!=' && $regs{$creg} != $cval + || $cop eq '<=' && $regs{$creg} <= $cval + || $cop eq '>=' && $regs{$creg} >= $cval) { + if ($op eq 'inc') { + $regs{$reg} += $val;; + } else { + $regs{$reg} -= $val; + } + } +} + +use List::Util qw(max); + +say max values %regs; diff --git a/2017/16.pl b/2017/16.pl new file mode 100755 index 0000000..8562c4f --- /dev/null +++ b/2017/16.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +use List::Util qw(max); + +my %regs; +my $max; +while (<>) { + chomp; + my ($reg, $op, $val, $creg, $cop, $cval) = + /^(\w+) (\S\Sc) (-?\d+) if (\w+) (\S+) (-?\d+)/; + $regs{$reg} //= 0; + $regs{$creg} //= 0; + say "$reg $op $val $creg $cop $cval"; + if ($cop eq '<' && $regs{$creg} < $cval + || $cop eq '>' && $regs{$creg} > $cval + || $cop eq '==' && $regs{$creg} == $cval + || $cop eq '!=' && $regs{$creg} != $cval + || $cop eq '<=' && $regs{$creg} <= $cval + || $cop eq '>=' && $regs{$creg} >= $cval) { + if ($op eq 'inc') { + $regs{$reg} += $val;; + } else { + $regs{$reg} -= $val; + } + } + my $m = max values %regs; + $max = $m if !$max || $max < $m; +} + +say $max; + + diff --git a/2017/17.pl b/2017/17.pl new file mode 100755 index 0000000..1a1f1a6 --- /dev/null +++ b/2017/17.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp (my $stream = <>); + +1 while $stream =~ s/<[^!>]*!./]*?>//; + +my $sum; +my $depth = 0; +for my $c (split //, $stream) { + if ($c eq '{') { + $depth++; + } elsif ($c eq '}') { + $sum += $depth--; + } +} + +say $sum; diff --git a/2017/18.pl b/2017/18.pl new file mode 100755 index 0000000..88fa689 --- /dev/null +++ b/2017/18.pl @@ -0,0 +1,12 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp (my $stream = <>); + +my $glen; +1 while $stream =~ s/<([^!>]*)!./<$1/; +1 while $stream =~ s/<([^>]*?)>/$glen += length $1/e; +say $glen; + diff --git a/2017/19.pl b/2017/19.pl new file mode 100755 index 0000000..2c5a9cb --- /dev/null +++ b/2017/19.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +my @l = split /[,\s]/, <>; + +my $pos = 0; +my $skip = 0; +# my @nodes = (0 .. 4); +my @nodes = (0 .. 255); +my $n = @nodes; + +for my $i (@l) { + my $end = $pos + $i; + my @to_rev; + say "pos=$pos skip $skip i=$i ", join(',', @nodes); + if ($end > $n) { + push @to_rev, splice @nodes, $pos; + push @to_rev, splice @nodes, 0, $end - $n; + @to_rev = reverse @to_rev; + say "to_rev = ", join(',', @to_rev); + unshift @nodes, splice @to_rev, @to_rev-($end-$n); + push @nodes, @to_rev; + } else { + push @to_rev, splice @nodes, $pos, $i; + splice @nodes, $pos, 0, reverse @to_rev; + } + $pos += $i + $skip++;; + $pos -= $n while $pos >= $n; +} + +say join(',',@nodes); + +say $nodes[0] * $nodes[1]; diff --git a/2017/20.pl b/2017/20.pl new file mode 100755 index 0000000..dadcfa8 --- /dev/null +++ b/2017/20.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use v5.30; +use strict; + +chomp(my $line = <>); +my @l = map { ord } split //, $line; +push @l, 17, 31, 73, 47, 23; + +my $pos = 0; +my $skip = 0; +# my @nodes = (0 .. 4); +my @nodes = (0 .. 255); +my $n = @nodes; + +for (1 .. 64) { +for my $i (@l) { + my $end = $pos + $i; + my @to_rev; + say "pos=$pos skip $skip i=$i ", join(',', @nodes); + if ($end > $n) { + push @to_rev, splice @nodes, $pos; + push @to_rev, splice @nodes, 0, $end - $n; + @to_rev = reverse @to_rev; + say "to_rev = ", join(',', @to_rev); + unshift @nodes, splice @to_rev, @to_rev-($end-$n); + push @nodes, @to_rev; + } else { + push @to_rev, splice @nodes, $pos, $i; + splice @nodes, $pos, 0, reverse @to_rev; + } + $pos += $i + $skip++; + $pos -= $n while $pos >= $n; +} +} + +my $hash = ''; +while (my @s = splice (@nodes, 0, 16)) { + my $x = 0; + $x ^= $_ for @s; + $hash .= sprintf("%02x", $x); +} +say $hash;