]> www.fi.muni.cz Git - aoc.git/blob - 2017/48.pl
Day 25: examining the input
[aoc.git] / 2017 / 48.pl
1 #!/usr/bin/perl
2
3 use v5.30;
4 use strict;
5
6 my %neigh;
7 while (<>) {
8         my ($src, $dst) = /(\d+)/g;
9         push @{ $neigh{$src} }, $dst;
10         push @{ $neigh{$dst} }, $src;
11 }
12
13 my %used;
14 my $max;
15 my $mlen;
16 sub walk {
17         my ($score, @path) = @_;
18         my $now = $path[-1];
19         if (!$mlen || $mlen < @path) {
20                 $mlen = @path;
21                 $max = $score;
22         } elsif ($mlen && $mlen == @path) {
23                 $max = $score if $max < $score;
24         }
25         # say "$score: ", join(' ', @path);
26         for my $next (@{ $neigh{$now} }) {
27                 next if $used{$now,$next} || $used{$next,$now};
28                 $used{$now,$next} = 1;
29                 walk ($score + $now + $next, @path, $next);
30                 delete $used{$now,$next};
31         }
32 }
33
34 walk(0, 0);
35 say $max;