]> www.fi.muni.cz Git - aoc.git/blob - 2016/34.pl
Day 25: examining the input
[aoc.git] / 2016 / 34.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use v5.30;
5
6 use Digest::MD5 qw(md5_hex);
7 my $in = 'pvhmgsws';
8
9 my @paths = [ 0, 0, '' ];
10
11 my $max;
12 while (@paths) {
13         my $p = shift @paths;
14         my ($x, $y, $path) = @$p;
15
16         if ($x == 3 && $y == 3)  {
17                 $max = length $path if !$max || $max < length $path;
18                 next;
19         }
20
21         my $h = md5_hex($in.$path);
22         if ($y > 0 && substr($h, 0, 1) =~ /[b-f]/) {
23                 push @paths, [ $x, $y-1, $path.'U' ];
24         }
25         if ($y < 3 && substr($h, 1, 1) =~ /[b-f]/) {
26                 push @paths, [ $x, $y+1, $path.'D' ];
27         }
28         if ($x > 0 && substr($h, 2, 1) =~ /[b-f]/) {
29                 push @paths, [ $x-1, $y, $path.'L' ];
30         }
31         if ($x < 3 && substr($h, 3, 1) =~ /[b-f]/) {
32                 push @paths, [ $x+1, $y, $path.'R' ];
33         }
34 }
35
36 say $max;