]> www.fi.muni.cz Git - aoc.git/blobdiff - 2016/26.pl
The rest of Year 2016
[aoc.git] / 2016 / 26.pl
diff --git a/2016/26.pl b/2016/26.pl
new file mode 100755 (executable)
index 0000000..f58b0bf
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/perl -w
+
+use strict;
+use v5.30;
+
+my $in = 1362;
+# my $in = 10;
+
+my %maze;
+$; = ',';
+sub is_wall {
+       my ($x, $y) = @_;
+       return $maze{$x,$y} if exists $maze{$x,$y};
+
+       my $sum = $in + $x*$x + 3*$x + 2*$x*$y + $y + $y*$y;
+       my $bin = sprintf("%b", $sum);
+       say "$x $y => $sum => $bin";
+       my $count = () = $bin =~ /1/g;
+       return $maze{$x,$y} = $count & 1;
+}
+
+say is_wall(0, 0);
+say is_wall(3, 5);
+say is_wall(9, 2);
+
+my %seen = ( "1,1" => 1 );
+my @paths = ( [ 1, 1, 0 ] );
+while (@paths) {
+       my $p = shift @paths;
+       my ($x, $y, $steps) = @$p;
+       for my $d ([0, 1], [0, -1], [1, 0], [-1, 0]) {
+               my $x1 = $x + $d->[0];
+               my $y1 = $y + $d->[1];
+               next if $x1 < 0 || $y1 < 0;
+               next if $seen{$x1,$y1};
+               next if is_wall($x1, $y1);
+               next if $steps >= 50;
+               $seen{$x1,$y1}++;
+               push @paths, [$x1, $y1, $steps+1];
+       }
+}
+say scalar keys %seen;