From 41b6cc0895fac78607eee76c51f8a59d282383df Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 11 Dec 2017 11:15:33 -0500 Subject: [PATCH] day15 --- 2015/15/1.pl | 37 +++++++++++++++++++++++++++++++++++++ 2015/15/2.pl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2015/15/input.txt | 4 ++++ 3 files changed, 85 insertions(+) create mode 100644 2015/15/1.pl create mode 100644 2015/15/2.pl create mode 100644 2015/15/input.txt diff --git a/2015/15/1.pl b/2015/15/1.pl new file mode 100644 index 0000000..aa1ef58 --- /dev/null +++ b/2015/15/1.pl @@ -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; + + +} diff --git a/2015/15/2.pl b/2015/15/2.pl new file mode 100644 index 0000000..58ca2c9 --- /dev/null +++ b/2015/15/2.pl @@ -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; +} diff --git a/2015/15/input.txt b/2015/15/input.txt new file mode 100644 index 0000000..6f33d01 --- /dev/null +++ b/2015/15/input.txt @@ -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