]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 18: bruteforcing part 2
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 18 Dec 2024 05:23:52 +0000 (06:23 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 18 Dec 2024 05:23:52 +0000 (06:23 +0100)
2024/35.pl [new file with mode: 0755]
2024/36.pl [new file with mode: 0755]

diff --git a/2024/35.pl b/2024/35.pl
new file mode 100755 (executable)
index 0000000..79bdc5a
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+# my ($xmax, $ymax) = (6, 6);
+my ($xmax, $ymax) = (70,70);
+
+my %map;
+while (<>) {
+       chomp;
+       $map{join(',', split /,/)}++;
+       # last if $. == 12;
+       last if $. == 1024;
+}
+
+my @q = [0, 0, 0];
+my %seen;
+while (@q) {
+       my ($cost, $x, $y) = @{ shift @q };
+       if ($x == $xmax && $y == $ymax) {
+               say $cost;
+               last;
+       }
+       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];
+       }
+}
+
diff --git a/2024/36.pl b/2024/36.pl
new file mode 100755 (executable)
index 0000000..ec3a0e3
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my ($xmax, $ymax) = (70,70);
+# my ($xmax, $ymax) = (6, 6);
+my %map;
+while (<>) {
+       chomp;
+       $map{join(',', split /,/)}++;
+       # if ($. > 12 && !is_path()) {
+       if ($. > 1024 && !is_path()) {
+               say $_;
+               last;
+       }
+}
+
+sub is_path {
+       my @q = [0, 0, 0];
+       my %seen;
+       while (@q) {
+               my ($cost, $x, $y) = @{ shift @q };
+               if ($x == $xmax && $y == $ymax) {
+                       return 1;
+               }
+               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];
+               }
+       }
+       return 0;
+}
+