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);
- $| = 1;
- use Math::Vector::Real;
- my ($Y,$X) = Math::Vector::Real->canonical_base(2);
- my %Dirs = ( '.' => [$Y, $X, -$Y, -$X],
- '>' => [ $X], '<' => [-$X], '^' => [-$Y], 'v' => [ $Y] );
- # Read in grid:
- my @Grid = map { chomp; [split(//)] } <>;
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- sub print_grid { say "\t", join( '', @$_ ) foreach (@Grid); }
- my $start = V(0,1);
- my $end = V($#Grid, $Grid[0]->$#* - 1);
- say "Start: $start";
- say "End: $end";
- $Grid[$start->[0]][$start->[1]] = 'S';
- my @queue = ([$start + $Y, $start => 1]);
- my $part1 = 0;
- my $time = 0;
- STEP:
- while (my $state = shift @queue) {
- my ($pos, %path) = @$state;
- say "At $pos (", scalar @queue, ")" if (++$time % 10000 == 0);
- if ($pos == $end) {
- if (scalar %path > $part1) {
- $part1 = scalar %path;
- say "Found $part1";
- }
- next STEP;
- }
- DIR:
- foreach my $d ($Dirs{&grid_at($pos)}->@*) {
- my $npos = $pos + $d;
- next DIR if (&grid_at($npos) eq '#');
- next DIR if (exists $path{$npos});
- push( @queue, [$npos, %path, $npos => 1] );
- }
- }
- say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement