From e330552b518f9b2fce78f7a8f45af5768bac5e97 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 22 Dec 2017 11:06:02 -0500 Subject: [PATCH] 2017-22 --- 21/1.pl | 84 +++++++++++++++++++++++++++++++++++++++ 21/input.txt | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ 22/1.pl | 38 ++++++++++++++++++ 22/2.pl | 46 ++++++++++++++++++++++ 22/input.txt | 25 ++++++++++++ 22/test | 3 ++ 6 files changed, 304 insertions(+) create mode 100644 21/1.pl create mode 100644 21/input.txt create mode 100644 22/1.pl create mode 100644 22/2.pl create mode 100644 22/input.txt create mode 100644 22/test diff --git a/21/1.pl b/21/1.pl new file mode 100644 index 0000000..edf35d3 --- /dev/null +++ b/21/1.pl @@ -0,0 +1,84 @@ +use 5.20.0; + +use Grid::Transform; + +my %transform; +my $i = 0; +while(<>) { + chomp; + s/\///g; + my ($from,$to) = split ' => '; + + my @from = split '', $from; + my $gt = Grid::Transform->new( \@from, rows => sqrt @from ); + + for my $t ( + map { $_, $_->copy->rotate90, $_->copy->rotate180, $_->copy->rotate270 } + map { $_, $_->copy->flip_vertical } $gt ) { + $transform{ join '', $t->grid} = $to; + } +} + +use DDP; +#p %transform; + +my @grid = ([qw/ . # . /], [qw/ . . # /], [qw/ # # # /] ); + +for( 1..5 ) { +my @new_grid; +if( not @grid % 2 ) { + for my $x ( 0..$#grid/2 ) { + for my $y ( 0..$#grid/2 ) { + transform_2(\@grid,\@new_grid,$x,$y); + } + } +} +else { + for my $x ( 0..$#grid/3 ) { + for my $y ( 0..$#grid/3 ) { + transform_3(\@grid,\@new_grid,$x,$y); + } + } +} + +say @$_ for @new_grid; +say "\n\n"; +@grid = @new_grid; +} + +say scalar grep { $_ eq '#' } map { @$_ } @grid; + +sub transform_2 { + my ( $grid, $n, $x, $y ) = @_; + warn "$x:$y"; + my $k; + for my $i ( 0..1 ) { + for my $j ( 0..1 ) { + $k .= $grid->[2*$x+$i][2*$y+$j]; + } + } + my @new = split '', $transform{$k}; + warn @new; + my ($r,$c) = ($x,$y); + for my $k ( 0..2 ) { + $n->[3*$r+$k] ||= []; + $n->[3*$r+$k][3*$c+$_] = shift @new for 0..2; + } +} + +sub transform_3 { + my ( $grid, $n, $x, $y ) = @_; + my $k; + for my $i ( 0..2 ) { + for my $j ( 0..2 ) { + $k .= $grid->[3*$x+$i][3*$y+$j]; + } + } + warn $k; + my @new = split '', $transform{$k}; + my ($r,$c) = ($x,$y); + for my $k ( 0..3 ) { + $n->[4*$x+$k] ||= []; + $n->[4*$x+$k][4*$c+$_] = shift @new for 0..3; + } +} diff --git a/21/input.txt b/21/input.txt new file mode 100644 index 0000000..bda8318 --- /dev/null +++ b/21/input.txt @@ -0,0 +1,108 @@ +../.. => .##/#../..# +#./.. => .##/#../### +##/.. => ..#/#.#/#.. +.#/#. => #../#../.#. +##/#. => .#./#../#.. +##/## => .##/.../.## +.../.../... => #.#./###./####/#..# +#../.../... => .###/####/##../#.## +.#./.../... => ###./.###/#..#/#.## +##./.../... => ..../..../.#../##.. +#.#/.../... => ...#/.##./..../##.. +###/.../... => ##../##../##.#/..## +.#./#../... => .#../###./##../#### +##./#../... => ####/##.#/..../..## +..#/#../... => ..#./####/...#/#.## +#.#/#../... => #.#./##../##../.##. +.##/#../... => ##../####/..#./...# +###/#../... => #..#/#.#./##.#/#.#. +.../.#./... => .#.#/..#./#.../.... +#../.#./... => ##../..##/..##/.#.. +.#./.#./... => ..../##../##../#.## +##./.#./... => ...#/##../#..#/.### +#.#/.#./... => ####/##.#/###./..## +###/.#./... => ..../...#/.###/.#.. +.#./##./... => #.#./#..#/.##./.#.# +##./##./... => .###/#.../#..#/#.#. +..#/##./... => .###/####/..../#.## +#.#/##./... => ...#/.###/.###/.### +.##/##./... => ..##/..##/.###/##.# +###/##./... => ####/#..#/####/#.#. +.../#.#/... => #.##/..#./.###/#.#. +#../#.#/... => ####/##.#/##.#/.... +.#./#.#/... => #.../...#/#.##/#..# +##./#.#/... => .#.#/##../##../.... +#.#/#.#/... => ##.#/#.../##../.#.. +###/#.#/... => ...#/###./.#.#/...# +.../###/... => .###/#.##/#.../###. +#../###/... => ..##/.#../.###/..#. +.#./###/... => ..../.##./#.##/#.## +##./###/... => .#.#/##.#/#.../#.#. +#.#/###/... => ..#./#.../#.#./.##. +###/###/... => ..##/.#.#/#..#/.##. +..#/.../#.. => ..##/.#../##.#/##.. +#.#/.../#.. => ..#./..../#.../...# +.##/.../#.. => .##./..##/####/#... +###/.../#.. => #.##/..../##../#.## +.##/#../#.. => .###/...#/###./.... +###/#../#.. => .#.#/#.#./#.##/..#. +..#/.#./#.. => ...#/..#./..##/.#.# +#.#/.#./#.. => #.../##.#/.###/#.#. +.##/.#./#.. => ###./####/#..#/##.# +###/.#./#.. => ..../..#./..../#... +.##/##./#.. => .#.#/.##./.#.#/#.## +###/##./#.. => ..../##../###./.#.# +#../..#/#.. => ...#/#.../#.##/.### +.#./..#/#.. => #..#/.#../###./#.#. +##./..#/#.. => #.#./..#./###./###. +#.#/..#/#.. => .#.#/##.#/##../#### +.##/..#/#.. => ###./..../.#../...# +###/..#/#.. => #.#./.##./.#.#/#..# +#../#.#/#.. => #.#./##.#/.#../.### +.#./#.#/#.. => ##.#/#.#./#.../#### +##./#.#/#.. => .#.#/#.../..#./#.## +..#/#.#/#.. => ##.#/.##./#.../.### +#.#/#.#/#.. => ..##/..../..../#### +.##/#.#/#.. => ####/#.#./###./.#.# +###/#.#/#.. => #.##/..#./##../#... +#../.##/#.. => ..##/##.#/####/.#.. +.#./.##/#.. => ..##/##../.#../..## +##./.##/#.. => ..##/.#.#/#..#/.... +#.#/.##/#.. => #.../##../...#/.#.# +.##/.##/#.. => ##../...#/.###/.#.# +###/.##/#.. => ####/..#./.##./#.## +#../###/#.. => .#.#/##.#/#.#./#.#. +.#./###/#.. => .###/#..#/.#.#/###. +##./###/#.. => ##../.#../###./.#.# +..#/###/#.. => #.##/..../...#/..#. +#.#/###/#.. => #.../#..#/..../.#.. +.##/###/#.. => ####/#..#/..#./.#.# +###/###/#.. => .##./##../.#../..#. +.#./#.#/.#. => #.#./.###/#.#./..## +##./#.#/.#. => .##./..../..##/##.. +#.#/#.#/.#. => ...#/..../.#.#/..## +###/#.#/.#. => .#../####/#.#./#.## +.#./###/.#. => #..#/.#.#/#..#/#.#. +##./###/.#. => .#../##../#..#/..## +#.#/###/.#. => #.#./.##./##.#/.#.# +###/###/.#. => #.#./...#/..##/#... +#.#/..#/##. => ..#./..#./...#/#..# +###/..#/##. => #..#/###./..../##.# +.##/#.#/##. => #.##/.#.#/...#/..## +###/#.#/##. => #.##/...#/.##./.### +#.#/.##/##. => ..../##.#/..../...# +###/.##/##. => .###/#.../###./###. +.##/###/##. => #.../#.#./.###/..#. +###/###/##. => #.##/.#../..#./.#.# +#.#/.../#.# => .##./##../###./.### +###/.../#.# => ..##/...#/###./.#.. +###/#../#.# => ##.#/..#./#.##/.#.. +#.#/.#./#.# => .#../#.##/...#/###. +###/.#./#.# => ..#./..../####/#### +###/##./#.# => ###./#..#/..../#..# +#.#/#.#/#.# => ##.#/###./..../#... +###/#.#/#.# => ##../.###/#..#/.#.. +#.#/###/#.# => #.../###./.###/..#. +###/###/#.# => ..../.##./.#../###. +###/#.#/### => ##../#.../.###/#... +###/###/### => .###/###./#.##/..#. diff --git a/22/1.pl b/22/1.pl new file mode 100644 index 0000000..03ca503 --- /dev/null +++ b/22/1.pl @@ -0,0 +1,38 @@ +use 5.20.0; + +use Math::Vector::Real; + +use experimental qw/ postderef signatures /; + +my @d = map { V(@$_) } [ -1,0], [0,1],[1,0],[0,-1]; +my $di = 0; + +my %grid; +my ($i,$j) = (0,0); +for my $r( <> ) { + $j = 0; + chomp $r; + for ( split '', $r ) { + $grid{$i}{$j++} = $_; + } + $i++; +} + +use DDP; +p %grid; + +my $coord = V(12,12); + + +my $sum; +for(1..10_000){ + my $infected = $grid{$coord->[0]}{$coord->[1]} eq '#'; + $di += $infected ? 1 : 3; + $di %= 4; + $grid{$coord->[0]}{$coord->[1]} = $infected ? '.' : '#'; + $sum++ unless $infected; + $coord += $d[$di]; +} + +say $sum; + diff --git a/22/2.pl b/22/2.pl new file mode 100644 index 0000000..a9abe34 --- /dev/null +++ b/22/2.pl @@ -0,0 +1,46 @@ +use 5.20.0; + +use Math::Vector::Real; + +use experimental qw/ postderef signatures /; + +my @d = map { V(@$_) } [ -1,0], [0,1],[1,0],[0,-1]; +my $di = 0; + +my %grid; +my ($i,$j) = (0,0); +for my $r( <> ) { + $j = 0; + chomp $r; + for ( split '', $r ) { + $grid{$i}{$j++} = $_ eq '#' ? 2 : 0; + } + $i++; +} + +use DDP; +p %grid; + +my $coord = V( ( 12 ) x 2); + +my @id = ( 3, 0, 1, 2 ); + +my $sum; +for(1..10000000){ + my $infected = $grid{$coord->[0]}{$coord->[1]}; + $di += $id[$infected]; + $di %= 4; + $grid{$coord->[0]}{$coord->[1]} = ($infected +1)%4; + $sum++ if $infected == 1; + $coord += $d[$di]; + # my %p = ( 0 => '.', 1 => 'W', 2 => 'I', 3 => 'F' ); + # for my $x ( -3..3 ) { + # for my $y ( -3..3 ) { + # print $p{ $grid{$x}{$y} }; + # } + # print "\n"; + # } +} + +say $sum; + diff --git a/22/input.txt b/22/input.txt new file mode 100644 index 0000000..ba1a696 --- /dev/null +++ b/22/input.txt @@ -0,0 +1,25 @@ +###.######..##.##..#..#.# +#.#.#.##.##.#####..##..#. +##...#.....#.#.#..##.#.## +....#####.#.#.#..###.###. +###.#.......#..#.#...#..# +.#.######.##.#.....#...## +##.#...#..#..#....##.#.#. +#.##..#..##.##..###...#.# +.#.......#.#..####.#.#.## +.#...###...##..#...#.#..# +...##......#.##.....#..#. +######....##...##.....#.# +.####..##..##.#.##.##..#. +.#.#...###.#....#.##.#### +..####..#.#..#.#.#......# +#.#..##..#####.#.#....##. +.....#..########....#.##. +##.###....#..###..#.....# +.#.##...#.#...###.##...#. +..#.##..#..####.##..###.# +.#..#.##..#.##...#####.#. +#..##............#..#.... +###.....#.##.#####...#.## +##.##..#.....##.......... +#.#..##.#.#..#....##..#.# diff --git a/22/test b/22/test new file mode 100644 index 0000000..3105a0b --- /dev/null +++ b/22/test @@ -0,0 +1,3 @@ +..# +#.. +...