From 86420d1bbd85b7f375e001073b0b27dbf0066f96 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 4 Dec 2017 19:57:15 -0500 Subject: [PATCH] day09 --- 2015/09/input.txt | 28 ++++++++++++++++++++++++++++ 2015/09/path.pl | 31 +++++++++++++++++++++++++++++++ 2015/09/path2.pl | 31 +++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 2015/09/input.txt create mode 100644 2015/09/path.pl create mode 100644 2015/09/path2.pl diff --git a/2015/09/input.txt b/2015/09/input.txt new file mode 100644 index 0000000..a56f5e2 --- /dev/null +++ b/2015/09/input.txt @@ -0,0 +1,28 @@ +AlphaCentauri to Snowdin = 66 +AlphaCentauri to Tambi = 28 +AlphaCentauri to Faerun = 60 +AlphaCentauri to Norrath = 34 +AlphaCentauri to Straylight = 34 +AlphaCentauri to Tristram = 3 +AlphaCentauri to Arbre = 108 +Snowdin to Tambi = 22 +Snowdin to Faerun = 12 +Snowdin to Norrath = 91 +Snowdin to Straylight = 121 +Snowdin to Tristram = 111 +Snowdin to Arbre = 71 +Tambi to Faerun = 39 +Tambi to Norrath = 113 +Tambi to Straylight = 130 +Tambi to Tristram = 35 +Tambi to Arbre = 40 +Faerun to Norrath = 63 +Faerun to Straylight = 21 +Faerun to Tristram = 57 +Faerun to Arbre = 83 +Norrath to Straylight = 9 +Norrath to Tristram = 50 +Norrath to Arbre = 60 +Straylight to Tristram = 27 +Straylight to Arbre = 81 +Tristram to Arbre = 90 diff --git a/2015/09/path.pl b/2015/09/path.pl new file mode 100644 index 0000000..614c391 --- /dev/null +++ b/2015/09/path.pl @@ -0,0 +1,31 @@ +use 5.20.0; + +use List::AllUtils qw/ uniq min /; + +use experimental qw/ signatures /; + +my %dist; +while(<>) { + my( $from, $to, $dist ) = split / (?:to|=) /; + $dist{$from}{$to} = $dist{$to}{$from} = $dist; +} + +my @cities = uniq keys %dist; + +say min map { + my @c = @cities; + min_distance( splice( @c, $_, 1 ), @c ) +} 0..$#cities; + +sub min_distance( $src, @left ) { + return 0 unless @left; + + return min map { + $dist{$src}{$left[ $_ ]} + min_distance( $left[$_], @left[0..$_-1], @left[$_+1..$#left] ) + } 0..$#left; + + + + + +} diff --git a/2015/09/path2.pl b/2015/09/path2.pl new file mode 100644 index 0000000..af7e7c2 --- /dev/null +++ b/2015/09/path2.pl @@ -0,0 +1,31 @@ +use 5.20.0; + +use List::AllUtils qw/ uniq max /; + +use experimental qw/ signatures /; + +my %dist; +while(<>) { + my( $from, $to, $dist ) = split / (?:to|=) /; + $dist{$from}{$to} = $dist{$to}{$from} = $dist; +} + +my @cities = uniq keys %dist; + +say max map { + my @c = @cities; + max_distance( splice( @c, $_, 1 ), @c ) +} 0..$#cities; + +sub max_distance( $src, @left ) { + return 0 unless @left; + + return max map { + $dist{$src}{$left[ $_ ]} + max_distance( $left[$_], @left[0..$_-1], @left[$_+1..$#left] ) + } 0..$#left; + + + + + +}