]> www.fi.muni.cz Git - aoc.git/blobdiff - 2017/47.pl
The rest of Year 2017
[aoc.git] / 2017 / 47.pl
diff --git a/2017/47.pl b/2017/47.pl
new file mode 100755 (executable)
index 0000000..5f22a43
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %neigh;
+while (<>) {
+       my ($src, $dst) = /(\d+)/g;
+       push @{ $neigh{$src} }, $dst;
+       push @{ $neigh{$dst} }, $src;
+}
+
+my %used;
+my $max;
+sub walk {
+       my ($score, @path) = @_;
+       my $now = $path[-1];
+       $max = $score if !$max || $max < $score;
+       say "$score: ", join(' ', @path);
+       for my $next (@{ $neigh{$now} }) {
+               next if $used{$now,$next} || $used{$next,$now};
+               $used{$now,$next} = 1;
+               walk ($score + $now + $next, @path, $next);
+               delete $used{$now,$next};
+       }
+}
+
+walk(0, 0);
+say $max;