This commit is contained in:
Yanick Champoux 2017-12-18 22:24:48 -05:00
parent 75821152c4
commit 8694868b3f
2 changed files with 72 additions and 3 deletions

View File

@ -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,[]);

43
2015/24/2.pl Normal file
View File

@ -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 );
}