This commit is contained in:
Yanick Champoux 2017-12-11 11:15:33 -05:00
parent 91a8bf6112
commit 41b6cc0895
3 changed files with 85 additions and 0 deletions

37
2015/15/1.pl Normal file
View File

@ -0,0 +1,37 @@
use 5.20.0;
use List::AllUtils qw/ pairmap max product sum /;
my %ingredients = pairmap {
$a => { $b =~ /(\w+) (-?\d+)/g }
} map { split ': ' } <>;
my @dim = qw/ capacity durability flavor texture /;
use DDP;
p %ingredients;
say max_score( 100, [ sort keys %ingredients ], {} );
use experimental 'signatures';
sub max_score( $spoons, $ingr, $mix ) {
unless($spoons) {
# use DDP; p $mix;
my $total;
return product
map { max $_, 0 }
map { my $d = $_; sum pairmap { $b * $ingredients{$a}{$d} } %$mix } @dim;
}
my( $i, @r ) = @$ingr;
return max_score(0,[],{ %$mix, $i => $spoons }) unless @r;
return max map {
max_score( $spoons - $_, \@r, { %$mix, $i => $_ } )
} 0..$spoons;
}

44
2015/15/2.pl Normal file
View File

@ -0,0 +1,44 @@
use 5.20.0;
use List::AllUtils qw/ pairmap max product sum /;
my %ingredients = pairmap {
$a => { $b =~ /(\w+) (-?\d+)/g }
} map { split ': ' } <>;
my @dim = qw/ capacity durability flavor texture /;
use DDP;
p %ingredients;
say max_score( 100, [ sort keys %ingredients ], {} );
use experimental 'signatures';
sub max_score( $spoons, $ingr, $mix ) {
return if calories(%$mix) > 500;
unless($spoons) {
# use DDP; p $mix;
return 0 if calories(%$mix) != 500;
my $total;
return product
map { max $_, 0 }
map { my $d = $_; sum pairmap { $b * $ingredients{$a}{$d} } %$mix } @dim;
}
my( $i, @r ) = @$ingr;
return max_score(0,[],{ %$mix, $i => $spoons }) unless @r;
return max map {
max_score( $spoons - $_, \@r, { %$mix, $i => $_ } )
} 0..$spoons;
}
sub calories( %mix ) {
return sum pairmap { $b * $ingredients{$a}{calories} } %mix;
}

4
2015/15/input.txt Normal file
View File

@ -0,0 +1,4 @@
Sprinkles: capacity 2, durability 0, flavor -2, texture 0, calories 3
Butterscotch: capacity 0, durability 5, flavor -3, texture 0, calories 3
Chocolate: capacity 0, durability 0, flavor 5, texture -1, calories 8
Candy: capacity 0, durability -1, flavor 0, texture 5, calories 8