From 8694868b3fca83267deb0b7d426d63cc75c5c4e1 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 18 Dec 2017 22:24:48 -0500 Subject: [PATCH] day24 --- 2015/24/1.pl | 32 +++++++++++++++++++++++++++++--- 2015/24/2.pl | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 2015/24/2.pl diff --git a/2015/24/1.pl b/2015/24/1.pl index 79f2e40..f7d4875 100644 --- a/2015/24/1.pl +++ b/2015/24/1.pl @@ -10,11 +10,38 @@ use experimental qw/ signatures postderef /; use DDP; +my @min = ( 1 ) x $goal; my $min = 1E99; -aggregate( \@gifts, [shift @gifts], [] ); + +aggregate( \@gifts, $goal ); -sub aggregate($gifts,$sofar,$next,$done=undef) { +sub aggregate($gifts,$goal,@sofar) { + use DDP; + # p @_; + + return if $goal < 0; + + return if @sofar > @min; + return if product(@sofar) > $min; + + if($goal == 0) { + @min = @sofar; + $min = product(@min); + say $min; + return; + } + + return unless @$gifts; + + my( $i, @gifts) = @$gifts; + + aggregate( \@gifts, $goal - $i, @sofar, $i ); + aggregate( \@gifts, $goal, @sofar ); + +} + +__END__ # p @_; my $total = sum $sofar->@*; @@ -49,7 +76,6 @@ sub aggregate($gifts,$sofar,$next,$done=undef) { } -__END__ my $min = 1E99; my @poss = possibilities(\@gifts,[]); diff --git a/2015/24/2.pl b/2015/24/2.pl new file mode 100644 index 0000000..f347ad6 --- /dev/null +++ b/2015/24/2.pl @@ -0,0 +1,43 @@ +use 5.20.0; + +my @gifts = map { 0+$_} <>; + +use List::AllUtils qw/ sum product min /; + +my $goal = sum(@gifts)/4; + +use experimental qw/ signatures postderef /; + +use DDP; + +my @min = ( 1 ) x $goal; +my $min = 1E99; + +aggregate( \@gifts, $goal ); + + +sub aggregate($gifts,$goal,@sofar) { + use DDP; + # p @_; + + return if $goal < 0; + + return if @sofar > @min; + return if product(@sofar) > $min; + + if($goal == 0) { + @min = @sofar; + $min = product(@min); + say $min; + return; + } + + return unless @$gifts; + + my( $i, @gifts) = @$gifts; + + aggregate( \@gifts, $goal - $i, @sofar, $i ); + aggregate( \@gifts, $goal, @sofar ); + +} +