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 constant GRID_SIZE => 7;
- #use constant TIME => 12;
- use constant GRID_SIZE => 71;
- use constant TIME => 1024;
- use Math::Vector::Real;
- my ($vy,$vx) = Math::Vector::Real->canonical_base(2);
- my @Dirs = ($vy, $vx, -$vy, -$vx);
- # Make grid, adding sentinel ~s to right and bottom
- my @Grid = map { [('.') x GRID_SIZE, '~'] } (1 .. GRID_SIZE);
- push( @Grid, [('~') x GRID_SIZE] );
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[1]][$p->[0]]) }
- sub print_grid { say "\t", join( '', @$_ ) foreach (@Grid); }
- # Read list of points:
- my @input = map { V(split /,/) } <>;
- # Drop up to TIME
- $Grid[$_->[1]][$_->[0]] = '#' foreach (splice( @input, 0, TIME ));
- my %visit;
- my @queue = ([V(0,0), 0]);
- my $end = V(GRID_SIZE - 1, GRID_SIZE - 1);
- while (my $state = shift @queue) {
- my ($pos, $time) = @$state;
- if ($pos == $end) {
- say "Part 1: $time";
- last;
- }
- next if ($visit{$pos}++);
- # Add legal moves to queue
- push(@queue, [$_, $time + 1]) foreach (grep {grid_at($_) eq '.'} map {$pos + $_} @Dirs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement