Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use List::AllUtils qw(sum);
- use Math::BigInt;
- $| = 1;
- $; = ',';
- my @Dirs = ([-1,-1], [-1, 0], [-1, 1],
- [ 0,-1], [ 0, 0], [ 0, 1],
- [ 1,-1], [ 1, 0], [ 1, 1]);
- # Read in cellular automata rules:
- $_ = <>;
- chomp;
- my @Map = map { int($_ eq '#') } split //;
- # Read in starting state:
- $_ = <>; # blank line
- my @Input = map { chomp; [map {int($_ eq '#')} split //] } <>;
- my $MAX_Y = $#Input;
- my $MAX_X = $Input[0]->$#*;
- # State of all points outside the bounding box
- my $Border = 0;
- # Load starting position into Grid:
- my %Grid;
- foreach my $y (0 .. $MAX_Y) {
- foreach my $x (0 .. $MAX_X) {
- $Grid{$y,$x} = $Input[$y][$x];
- }
- }
- sub pixel_count {
- return ($Border ? Math::BigInt->binf() : sum map { $Grid{$_} } keys %Grid);
- }
- foreach my $t (1 .. 50) {
- print ::stderr "Time: $t\r";
- my %next;
- foreach my $y (-$t .. $MAX_Y + $t) {
- foreach my $x (-$t .. $MAX_X + $t) {
- my $idx = 0;
- foreach my $neigh (map {[$y + $_->[0], $x + $_->[1]]} @Dirs) {
- $idx = 2 * $idx + ($Grid{ join($;, @$neigh) } // $Border);
- }
- $next{$y,$x} = $Map[$idx];
- }
- }
- %Grid = %next;
- $Border = $Map[511 * $Border];
- print( "\nPart 1: ", &pixel_count, "\n" ) if ($t == 2);
- }
- print( "\nPart 2: ", &pixel_count, "\n" );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement