From: Jan "Yenya" Kasprzak Date: Tue, 11 Jan 2022 23:19:08 +0000 (+0100) Subject: Year 2016, days 1-10: so far pretty interesting X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc.git;a=commitdiff_plain;h=0f4194cbf2d35e7423cecf8d0b0c029ef2992ade Year 2016, days 1-10: so far pretty interesting --- diff --git a/2016/01.pl b/2016/01.pl new file mode 100755 index 0000000..42982f0 --- /dev/null +++ b/2016/01.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my @dir = ([0, -1], [1, 0], [0, 1], [-1, 0]); +my $d = 0; + +my ($x, $y) = (0, 0); + +$_ = <>; +while (/([LR])(\d+)/g) { + if ($1 eq 'L') { + $d = $#dir if --$d < 0; + } else { + $d = 0 if ++$d > $#dir; + } + $x += $2 * $dir[$d]->[0]; + $y += $2 * $dir[$d]->[1]; +} + +say abs($x)+abs($y); + diff --git a/2016/02.pl b/2016/02.pl new file mode 100755 index 0000000..a860be8 --- /dev/null +++ b/2016/02.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my @dir = ([0, -1], [1, 0], [0, 1], [-1, 0]); +my $d = 0; + +my ($x, $y) = (0, 0); + +$_ = <>; +my %seen; +while (/([LR])(\d+)/g) { + if ($1 eq 'L') { + $d = $#dir if --$d < 0; + } else { + $d = 0 if ++$d > $#dir; + } + my $i = $2; + while ($i--) { + $x += $dir[$d]->[0]; + $y += $dir[$d]->[1]; + if ($seen{$x,$y}++) { + say abs($x)+abs($y); + exit 0; + } + } +} + + diff --git a/2016/03.pl b/2016/03.pl new file mode 100755 index 0000000..ca698da --- /dev/null +++ b/2016/03.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +$; = ','; +my %pos2num = ( + "0,0" => 1, + "1,0" => 2, + "2,0" => 3, + "0,1" => 4, + "1,1" => 5, + "2,1" => 6, + "0,2" => 7, + "1,2" => 8, + "2,2" => 9, +); +my @pos = (1, 1); +my $ret = ''; +while (<>) { + chomp; + for my $step (split //) { + $pos[0]-- if $pos[0] && $step eq 'L'; + $pos[0]++ if $pos[0] < 2 && $step eq 'R'; + $pos[1]-- if $pos[1] && $step eq 'U'; + $pos[1]++ if $pos[1] < 2 && $step eq 'D'; + } + $ret .= $pos2num{$pos[0],$pos[1]}; +} + +say $ret; + diff --git a/2016/04.pl b/2016/04.pl new file mode 100755 index 0000000..aa06da1 --- /dev/null +++ b/2016/04.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +$; = ','; +my %pos2num = ( + "2,0" => 1, + "1,1" => 2, + "2,1" => 3, + "3,1" => 4, + "0,2" => 5, + "1,2" => 6, + "2,2" => 7, + "3,2" => 8, + "4,2" => 9, + "1,3" => 'A', + "2,3" => 'B', + "3,3" => 'C', + "2,4" => 'D', +); +my @pos = (0, 2); +my $ret = ''; +while (<>) { + chomp; + for my $step (split //) { + $pos[0]-- if $pos2num{ ($pos[0]-1).",$pos[1]" } && $step eq 'L'; + $pos[0]++ if $pos2num{ ($pos[0]+1).",$pos[1]" } && $step eq 'R'; + $pos[1]-- if $pos2num{ "$pos[0],".($pos[1]-1) } && $step eq 'U'; + $pos[1]++ if $pos2num{ "$pos[0],".($pos[1]+1) } && $step eq 'D'; + } + $ret .= $pos2num{$pos[0],$pos[1]}; +} + +say $ret; + diff --git a/2016/05.pl b/2016/05.pl new file mode 100755 index 0000000..9d52104 --- /dev/null +++ b/2016/05.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my $valid; +while (<>) { + chomp; + my (@d) = sort { $a <=> $b } /(\d+)/g; + $valid++ if $d[0]+$d[1] > $d[2]; +} + +say $valid; diff --git a/2016/06.pl b/2016/06.pl new file mode 100755 index 0000000..d11ed98 --- /dev/null +++ b/2016/06.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my $valid; +my @d; +while (<>) { + chomp; + push @d, [ /(\d+)/g ]; + if (@d == 3) { + for my $c (0 .. 2) { + my @t = sort { $a <=> $b } ($d[0][$c], $d[1][$c], $d[2][$c]); + $valid++ if $t[0]+$t[1] > $t[2]; + } + @d = (); + } +} + +say $valid; diff --git a/2016/07.pl b/2016/07.pl new file mode 100755 index 0000000..fd8ba32 --- /dev/null +++ b/2016/07.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my $sum; +while (<>) { + chomp; + my ($code, $id, $csum) = /\A([a-z-]+)-(\d+)\[([a-z]{5})\]/; + my %hist; + $hist{$&}++ while ($code =~ /[a-z]/g); + my $c1; + for my $l (sort { $hist{$b} <=> $hist{$a} || $a cmp $b } keys %hist) { + $c1 .= $l; + last if length $c1 >= length $csum; + } + $sum += $id if $csum eq $c1; +} +say $sum; diff --git a/2016/08.pl b/2016/08.pl new file mode 100755 index 0000000..80fa433 --- /dev/null +++ b/2016/08.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my $sum; +while (<>) { + chomp; + my ($code, $id, $csum) = /\A([a-z-]+)-(\d+)\[([a-z]{5})\]/; + my $d; + $code =~ s/-/ /g; + for (split //, $code) { + if ($_ eq ' ') { + $d .= ' '; + next; + } + $d .= chr(ord('a') + ((ord($_)-ord('a')+$id) % 26)); + } + say $id if $d =~ /pole/; +} diff --git a/2016/09.pl b/2016/09.pl new file mode 100755 index 0000000..6952777 --- /dev/null +++ b/2016/09.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +use Digest::MD5 qw(md5_hex); + +my $id = 'ugkcyxxp'; +my $pass = ''; +my $i = 0; +while (length($pass) < 8) { + my $h = md5_hex($id.$i); + if ($h =~ /\A00000/) { + $pass .= substr($h, 5, 1); + } + $i++; +} + +say $pass; + diff --git a/2016/10.pl b/2016/10.pl new file mode 100755 index 0000000..54a7009 --- /dev/null +++ b/2016/10.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +use Digest::MD5 qw(md5_hex); + +my $id = 'ugkcyxxp'; +my $pass = ' ' x 8; +my $i = 0; +while ($pass =~ /\s/) { + my $h = md5_hex($id.$i); + if ($h =~ /\A00000/) { + my $pos = substr($h, 5, 1); + if ($pos =~ /[0-7]/ && substr($pass, $pos, 1) eq ' ') { + substr($pass, $pos, 1) = substr($h, 6, 1); + } + } + $i++; +} + +say $pass; + diff --git a/2016/11.pl b/2016/11.pl new file mode 100755 index 0000000..bb43630 --- /dev/null +++ b/2016/11.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my %h; +while (<>) { + chomp; + my $i = 0; + for my $l (split //) { + $h{$i++}{$l}++; + } +} + +my $p; + +for my $i (0..7) { + my @a = sort { $h{$i}{$b} <=> $h{$i}{$a} } keys %{ $h{$i} }; + $p .= $a[0]; +} + +say $p; + + diff --git a/2016/12.pl b/2016/12.pl new file mode 100755 index 0000000..7d7d68d --- /dev/null +++ b/2016/12.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my %h; +while (<>) { + chomp; + my $i = 0; + for my $l (split //) { + $h{$i++}{$l}++; + } +} + +my $p; + +for my $i (0..7) { + my @a = sort { $h{$i}{$b} <=> $h{$i}{$a} } keys %{ $h{$i} }; + $p .= $a[-1]; +} + +say $p; + + diff --git a/2016/13.pl b/2016/13.pl new file mode 100755 index 0000000..8809677 --- /dev/null +++ b/2016/13.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my $count; + +while (<>) { + chomp; + next if /\[[^\]]*(\w)(?!\1)(\w)\2\1/; + next if !/(\w)(?!\1)(\w)\2\1/; + $count++; + say; +} + +say $count; diff --git a/2016/14.pl b/2016/14.pl new file mode 100755 index 0000000..4694715 --- /dev/null +++ b/2016/14.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my $count; + +line: +while (<>) { + chomp; + my $super = $_; + $super =~ s/\[.*?\]/---/g; + my $hyper = join('---', ($_ =~ /\[([^\]]*)\]/g)); + while ($super =~ /(\w)(?!\1)(\w)\1/g) { + pos($super)-= 2; + my $s = "$2$1$2"; + if ($hyper =~ /$s/) { + $count++; + next line; + } + } +} + +say $count; diff --git a/2016/15.pl b/2016/15.pl new file mode 100755 index 0000000..c8fbd7c --- /dev/null +++ b/2016/15.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my %screen; +my ($w, $h) = (50, 6); +$; = ','; + +while (<>) { + if (/rect (\d+)x(\d+)/) { + for my $x (0 .. $1-1) { + for my $y (0 .. $2-1) { + $screen{$x,$y} = 1; + } } + } elsif (/rotate row y=(\d+) by (\d+)/) { + my %ns = %screen; + for my $x (0 .. $w-1) { + my $sx = $x - $2; + $sx += $w if $sx < 0; + $ns{$x,$1} = $screen{$sx,$1}; + } + %screen = %ns; + } elsif (/rotate column x=(\d+) by (\d+)/) { + my %ns = %screen; + for my $y (0 .. $h-1) { + my $sy = $y - $2; + $sy += $h if $sy < 0; + $ns{$1,$y} = $screen{$1,$sy}; + } + %screen = %ns; + } +} + +my $sum =()= grep { $screen{$_} } keys %screen; + +say $sum; + diff --git a/2016/16.pl b/2016/16.pl new file mode 100755 index 0000000..4e68ebe --- /dev/null +++ b/2016/16.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my %screen; +my ($w, $h) = (50, 6); +$; = ','; + +while (<>) { + if (/rect (\d+)x(\d+)/) { + for my $x (0 .. $1-1) { + for my $y (0 .. $2-1) { + $screen{$x,$y} = 1; + } } + } elsif (/rotate row y=(\d+) by (\d+)/) { + my %ns = %screen; + for my $x (0 .. $w-1) { + my $sx = $x - $2; + $sx += $w if $sx < 0; + $ns{$x,$1} = $screen{$sx,$1}; + } + %screen = %ns; + } elsif (/rotate column x=(\d+) by (\d+)/) { + my %ns = %screen; + for my $y (0 .. $h-1) { + my $sy = $y - $2; + $sy += $h if $sy < 0; + $ns{$1,$y} = $screen{$1,$sy}; + } + %screen = %ns; + } +} + +for my $y (0 .. $h-1) { + for my $x (0 .. $w-1) { + print $screen{$x,$y} ? '#' : ' '; + } + print "\n"; +} + diff --git a/2016/17.pl b/2016/17.pl new file mode 100755 index 0000000..768cda1 --- /dev/null +++ b/2016/17.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +chomp (my $in = <>); +while ($in =~ /\((\d+)x(\d+)\)/g) { + my $p = pos($in) - length("($1x$2)"); + my $str = substr($in, pos($in), $1); + my $repl = $str x $2; + say "repl=$repl"; + substr($in, $p, length($str)+length("($1x$2)")) = $repl; + pos($in) = $p + length $repl; +} + +say $in; +say length $in; diff --git a/2016/18.pl b/2016/18.pl new file mode 100755 index 0000000..c1eef09 --- /dev/null +++ b/2016/18.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +sub str_len { + my ($in) = @_; + my $l = 0; + while ($in) { + $l++ while $in =~ s/\A[A-Z]//; + next if $in !~ /\A\((\d+)x(\d+)\)/; + my $str = substr($in, length("($1x$2)"), $1); + substr($in, 0, length("($1x$2)") + $1) = ''; + my $c = $2; + $l += $c * str_len($str); + } + return $l; +} + +chomp (my $in = <>); +say str_len($in); diff --git a/2016/19.pl b/2016/19.pl new file mode 100755 index 0000000..efe1e0d --- /dev/null +++ b/2016/19.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my @bot_has; +my @todo; +my @outp; +my @rule; +while (<>) { + chomp; + if (/value (\d+) goes to bot (\d+)/) { + push @{ $bot_has[$2] }, $1; + push @todo, $2 if @{ $bot_has[$2] } == 2; + } elsif (/bot (\d+) gives low to (\w+) (\d+) and high to (\w+) (\d+)/) { + $rule[$1] = [ $2, $3, $4, $5 ]; + } else { + die $_; + } +} + +while (my $bot = shift @todo) { + my ($lo, $hi) = sort { $a <=> $b } @{ $bot_has[$bot] }; + if ($lo == 17 && $hi == 61) { + say "$bot"; + last; + } + my @r = @{ $rule[$bot] }; + if ($r[0] eq 'output') { + $outp[$r[1]] = $lo; + } else { + push @{ $bot_has[$r[1]] }, $lo; + push @todo, $r[1] if @{ $bot_has[$r[1]] } == 2; + } + + if ($r[2] eq 'output') { + $outp[$r[3]] = $hi; + } else { + push @{ $bot_has[$r[3]] }, $hi; + push @todo, $r[3] if @{ $bot_has[$r[3]] } == 2; + } +} + + diff --git a/2016/20.pl b/2016/20.pl new file mode 100755 index 0000000..15483c7 --- /dev/null +++ b/2016/20.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl -w + +use strict; +use v5.30; + +my @bot_has; +my @todo; +my @outp; +my @rule; +while (<>) { + chomp; + if (/value (\d+) goes to bot (\d+)/) { + push @{ $bot_has[$2] }, $1; + push @todo, $2 if @{ $bot_has[$2] } == 2; + } elsif (/bot (\d+) gives low to (\w+) (\d+) and high to (\w+) (\d+)/) { + $rule[$1] = [ $2, $3, $4, $5 ]; + } else { + die $_; + } +} + +while (defined(my $bot = shift @todo)) { + my ($lo, $hi) = sort { $a <=> $b } @{ $bot_has[$bot] }; + say "evaluating bot $bot which has $lo and $hi"; + if ($lo == 17 && $hi == 61) { + say "$bot"; + } + my @r = @{ $rule[$bot] }; + if ($r[0] eq 'output') { + $outp[$r[1]] = $lo; + say "output $r[1] => $lo"; + } else { + push @{ $bot_has[$r[1]] }, $lo; + push @todo, $r[1] if @{ $bot_has[$r[1]] } == 2; + } + + if ($r[2] eq 'output') { + $outp[$r[3]] = $hi; + say "output $r[3] => $hi"; + } else { + push @{ $bot_has[$r[3]] }, $hi; + push @todo, $r[3] if @{ $bot_has[$r[3]] } == 2; + } +} + +say $outp[0] * $outp[1] * $outp[2]; +