day15
This commit is contained in:
parent
91a8bf6112
commit
41b6cc0895
37
2015/15/1.pl
Normal file
37
2015/15/1.pl
Normal 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
44
2015/15/2.pl
Normal 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
4
2015/15/input.txt
Normal 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
|
Loading…
Reference in New Issue
Block a user