This commit is contained in:
Yanick Champoux 2017-12-22 11:06:02 -05:00
parent a1a18c063e
commit e330552b51
6 changed files with 304 additions and 0 deletions

84
21/1.pl Normal file
View File

@ -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;
}
}

108
21/input.txt Normal file
View File

@ -0,0 +1,108 @@
../.. => .##/#../..#
#./.. => .##/#../###
##/.. => ..#/#.#/#..
.#/#. => #../#../.#.
##/#. => .#./#../#..
##/## => .##/.../.##
.../.../... => #.#./###./####/#..#
#../.../... => .###/####/##../#.##
.#./.../... => ###./.###/#..#/#.##
##./.../... => ..../..../.#../##..
#.#/.../... => ...#/.##./..../##..
###/.../... => ##../##../##.#/..##
.#./#../... => .#../###./##../####
##./#../... => ####/##.#/..../..##
..#/#../... => ..#./####/...#/#.##
#.#/#../... => #.#./##../##../.##.
.##/#../... => ##../####/..#./...#
###/#../... => #..#/#.#./##.#/#.#.
.../.#./... => .#.#/..#./#.../....
#../.#./... => ##../..##/..##/.#..
.#./.#./... => ..../##../##../#.##
##./.#./... => ...#/##../#..#/.###
#.#/.#./... => ####/##.#/###./..##
###/.#./... => ..../...#/.###/.#..
.#./##./... => #.#./#..#/.##./.#.#
##./##./... => .###/#.../#..#/#.#.
..#/##./... => .###/####/..../#.##
#.#/##./... => ...#/.###/.###/.###
.##/##./... => ..##/..##/.###/##.#
###/##./... => ####/#..#/####/#.#.
.../#.#/... => #.##/..#./.###/#.#.
#../#.#/... => ####/##.#/##.#/....
.#./#.#/... => #.../...#/#.##/#..#
##./#.#/... => .#.#/##../##../....
#.#/#.#/... => ##.#/#.../##../.#..
###/#.#/... => ...#/###./.#.#/...#
.../###/... => .###/#.##/#.../###.
#../###/... => ..##/.#../.###/..#.
.#./###/... => ..../.##./#.##/#.##
##./###/... => .#.#/##.#/#.../#.#.
#.#/###/... => ..#./#.../#.#./.##.
###/###/... => ..##/.#.#/#..#/.##.
..#/.../#.. => ..##/.#../##.#/##..
#.#/.../#.. => ..#./..../#.../...#
.##/.../#.. => .##./..##/####/#...
###/.../#.. => #.##/..../##../#.##
.##/#../#.. => .###/...#/###./....
###/#../#.. => .#.#/#.#./#.##/..#.
..#/.#./#.. => ...#/..#./..##/.#.#
#.#/.#./#.. => #.../##.#/.###/#.#.
.##/.#./#.. => ###./####/#..#/##.#
###/.#./#.. => ..../..#./..../#...
.##/##./#.. => .#.#/.##./.#.#/#.##
###/##./#.. => ..../##../###./.#.#
#../..#/#.. => ...#/#.../#.##/.###
.#./..#/#.. => #..#/.#../###./#.#.
##./..#/#.. => #.#./..#./###./###.
#.#/..#/#.. => .#.#/##.#/##../####
.##/..#/#.. => ###./..../.#../...#
###/..#/#.. => #.#./.##./.#.#/#..#
#../#.#/#.. => #.#./##.#/.#../.###
.#./#.#/#.. => ##.#/#.#./#.../####
##./#.#/#.. => .#.#/#.../..#./#.##
..#/#.#/#.. => ##.#/.##./#.../.###
#.#/#.#/#.. => ..##/..../..../####
.##/#.#/#.. => ####/#.#./###./.#.#
###/#.#/#.. => #.##/..#./##../#...
#../.##/#.. => ..##/##.#/####/.#..
.#./.##/#.. => ..##/##../.#../..##
##./.##/#.. => ..##/.#.#/#..#/....
#.#/.##/#.. => #.../##../...#/.#.#
.##/.##/#.. => ##../...#/.###/.#.#
###/.##/#.. => ####/..#./.##./#.##
#../###/#.. => .#.#/##.#/#.#./#.#.
.#./###/#.. => .###/#..#/.#.#/###.
##./###/#.. => ##../.#../###./.#.#
..#/###/#.. => #.##/..../...#/..#.
#.#/###/#.. => #.../#..#/..../.#..
.##/###/#.. => ####/#..#/..#./.#.#
###/###/#.. => .##./##../.#../..#.
.#./#.#/.#. => #.#./.###/#.#./..##
##./#.#/.#. => .##./..../..##/##..
#.#/#.#/.#. => ...#/..../.#.#/..##
###/#.#/.#. => .#../####/#.#./#.##
.#./###/.#. => #..#/.#.#/#..#/#.#.
##./###/.#. => .#../##../#..#/..##
#.#/###/.#. => #.#./.##./##.#/.#.#
###/###/.#. => #.#./...#/..##/#...
#.#/..#/##. => ..#./..#./...#/#..#
###/..#/##. => #..#/###./..../##.#
.##/#.#/##. => #.##/.#.#/...#/..##
###/#.#/##. => #.##/...#/.##./.###
#.#/.##/##. => ..../##.#/..../...#
###/.##/##. => .###/#.../###./###.
.##/###/##. => #.../#.#./.###/..#.
###/###/##. => #.##/.#../..#./.#.#
#.#/.../#.# => .##./##../###./.###
###/.../#.# => ..##/...#/###./.#..
###/#../#.# => ##.#/..#./#.##/.#..
#.#/.#./#.# => .#../#.##/...#/###.
###/.#./#.# => ..#./..../####/####
###/##./#.# => ###./#..#/..../#..#
#.#/#.#/#.# => ##.#/###./..../#...
###/#.#/#.# => ##../.###/#..#/.#..
#.#/###/#.# => #.../###./.###/..#.
###/###/#.# => ..../.##./.#../###.
###/#.#/### => ##../#.../.###/#...
###/###/### => .###/###./#.##/..#.

38
22/1.pl Normal file
View File

@ -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;

46
22/2.pl Normal file
View File

@ -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;

25
22/input.txt Normal file
View File

@ -0,0 +1,25 @@
###.######..##.##..#..#.#
#.#.#.##.##.#####..##..#.
##...#.....#.#.#..##.#.##
....#####.#.#.#..###.###.
###.#.......#..#.#...#..#
.#.######.##.#.....#...##
##.#...#..#..#....##.#.#.
#.##..#..##.##..###...#.#
.#.......#.#..####.#.#.##
.#...###...##..#...#.#..#
...##......#.##.....#..#.
######....##...##.....#.#
.####..##..##.#.##.##..#.
.#.#...###.#....#.##.####
..####..#.#..#.#.#......#
#.#..##..#####.#.#....##.
.....#..########....#.##.
##.###....#..###..#.....#
.#.##...#.#...###.##...#.
..#.##..#..####.##..###.#
.#..#.##..#.##...#####.#.
#..##............#..#....
###.....#.##.#####...#.##
##.##..#.....##..........
#.#..##.#.#..#....##..#.#

3
22/test Normal file
View File

@ -0,0 +1,3 @@
..#
#..
...