my %map;
while (<>) {
chomp;
- $map{join(',', split /,/)}++;
+ $map{$_}++;
# last if $. == 12;
last if $. == 1024;
}
push @q, [$cost+1, $nx, $ny];
}
}
-
my %map;
while (<>) {
chomp;
- $map{join(',', split /,/)}++;
+ $map{$_}++;
# if ($. > 12 && !is_path()) {
if ($. > 1024 && !is_path()) {
say $_;
}
sub is_path {
- my @q = [0, 0, 0];
+ my @q = [0, 0];
my %seen;
while (@q) {
- my ($cost, $x, $y) = @{ shift @q };
- if ($x == $xmax && $y == $ymax) {
- return 1;
- }
+ my ($x, $y) = @{ shift @q };
+ return 1 if $x == $xmax && $y == $ymax;
+
for my ($dx, $dy) (-1, 0, 1, 0, 0, -1, 0, 1) {
my ($nx, $ny) = ($x+$dx, $y+$dy);
next if $map{"$nx,$ny"};
next if $seen{"$nx,$ny"}++;
next if $nx < 0 || $nx > $xmax || $ny < 0 || $ny > $ymax;
- push @q, [$cost+1, $nx, $ny];
+ push @q, [$nx, $ny];
}
}
return 0;