day09
This commit is contained in:
parent
1c92538c76
commit
86420d1bbd
28
2015/09/input.txt
Normal file
28
2015/09/input.txt
Normal file
@ -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
|
31
2015/09/path.pl
Normal file
31
2015/09/path.pl
Normal file
@ -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;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
31
2015/09/path2.pl
Normal file
31
2015/09/path2.pl
Normal file
@ -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;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user