Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use Math::Polynomial;
- $| = 1;
- my $TARGET = 26501365;
- my @Grid = map { chomp; [split(//)] } <>;
- my @Dirs = ([1,0], [0,1], [-1,0], [0,-1]);
- sub print_grid { say join( '', @$_ ) foreach (@Grid); }
- # Assume square grid, and start in middle:
- my $size = scalar( @Grid );
- my @pos = (int($size / 2), int($size / 2));
- die "S not in middle" if ($Grid[$pos[0]][$pos[1]] ne 'S');
- my @steps = (0);
- my %curr = (join( $;, @pos ) => 1);
- foreach my $time (1 .. ($TARGET % $size) + 2 * $size) {
- my %next;
- print ::stderr "$time\r";
- foreach my $l (keys %curr) {
- my @pt = split( $;, $l );
- foreach my $d (@Dirs) {
- my $ny = $pt[0] + $d->[0];
- my $nx = $pt[1] + $d->[1];
- $next{$ny,$nx}++ if ($Grid[$ny % $size][$nx % $size] ne '#');
- }
- }
- %curr = %next;
- push( @steps, scalar keys %curr );
- say "Part 1: $steps[-1]" if ($time == 64);
- }
- my @x = map {$TARGET % $size + $_ * $size} (0, 1, 2);
- my $fact = $size * $size;
- my $f = Math::Polynomial->interpolate( \@x, [map{$fact * $steps[$_]} @x] );
- say "f(x) = $f / ", $fact;
- say "Part 2: ", $f->evaluate( $TARGET ) / $fact;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement