]> www.fi.muni.cz Git - aoc.git/blobdiff - 2016/34.pl
The rest of Year 2016
[aoc.git] / 2016 / 34.pl
diff --git a/2016/34.pl b/2016/34.pl
new file mode 100755 (executable)
index 0000000..3e339a3
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/perl -w
+
+use strict;
+use v5.30;
+
+use Digest::MD5 qw(md5_hex);
+my $in = 'pvhmgsws';
+
+my @paths = [ 0, 0, '' ];
+
+my $max;
+while (@paths) {
+       my $p = shift @paths;
+       my ($x, $y, $path) = @$p;
+
+       if ($x == 3 && $y == 3)  {
+               $max = length $path if !$max || $max < length $path;
+               next;
+       }
+
+       my $h = md5_hex($in.$path);
+       if ($y > 0 && substr($h, 0, 1) =~ /[b-f]/) {
+               push @paths, [ $x, $y-1, $path.'U' ];
+       }
+       if ($y < 3 && substr($h, 1, 1) =~ /[b-f]/) {
+               push @paths, [ $x, $y+1, $path.'D' ];
+       }
+       if ($x > 0 && substr($h, 2, 1) =~ /[b-f]/) {
+               push @paths, [ $x-1, $y, $path.'L' ];
+       }
+       if ($x < 3 && substr($h, 3, 1) =~ /[b-f]/) {
+               push @paths, [ $x+1, $y, $path.'R' ];
+       }
+}
+
+say $max;