diff options
| author | andrezgz <andrezgz@gmail.com> | 2019-11-28 09:03:10 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2019-11-28 09:03:10 -0300 |
| commit | 7650205d5d522cdd6cd96dfcc90f3baec624140a (patch) | |
| tree | 3789315d9af5ce080b4a5967116b21cd3f6c67b5 /challenge-036 | |
| parent | df242af9cd77b0c87dd0488f2c469860eb7de1eb (diff) | |
| download | perlweeklychallenge-club-7650205d5d522cdd6cd96dfcc90f3baec624140a.tar.gz perlweeklychallenge-club-7650205d5d522cdd6cd96dfcc90f3baec624140a.tar.bz2 perlweeklychallenge-club-7650205d5d522cdd6cd96dfcc90f3baec624140a.zip | |
challenge-036 andrezgz task 2 minor fixes
Diffstat (limited to 'challenge-036')
| -rw-r--r-- | challenge-036/andrezgz/perl5/ch-2.pl | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/challenge-036/andrezgz/perl5/ch-2.pl b/challenge-036/andrezgz/perl5/ch-2.pl index a5679d316c..07bfff3198 100644 --- a/challenge-036/andrezgz/perl5/ch-2.pl +++ b/challenge-036/andrezgz/perl5/ch-2.pl @@ -19,9 +19,14 @@ use strict; use warnings; +# You can change the knapsack capacity in kg use constant MAX_WEIGHT => 15; + +# I've assumed you can choose each box only once, +# but you can change that... use constant REPEAT_BOX_LIMIT => 1; +# You can change the number of boxes, its weight in kg and amount of £ my $boxes_data = { R => { w => 1 , a => 1 }, B => { w => 1 , a => 2 }, @@ -30,6 +35,8 @@ my $boxes_data = { P => { w => 4 , a => 10 } }; +# I've assumed you can choose as much boxes as exist by default +# but you can change that with an argument to the script call my $max_boxes = shift || scalar keys %$boxes_data; my @best; @@ -37,7 +44,7 @@ my $boxes = { map { $_ => 1 } keys %$boxes_data }; sequence($_,$boxes) for keys %$boxes; # The first combination found that maximizes the amount of money -print 'Optimal combination: ' . (join ',', @best) . $/; +print 'Optimal combination: ' . (join ',', sort @best) . $/; print 'Knapsack weight: ' . weight(@best).' kg' . $/; print 'Knapsack amount: £' . amount(@best) . $/; @@ -78,15 +85,23 @@ sub sum_data { __END__ ./ch-2.pl -Optimal combination: P,R,G,B +Optimal combination: B,G,P,R Knapsack weight: 8 kg Knapsack amount: £15 ./ch-2.pl 3 -Optimal combination: P,B,G +Optimal combination: B,G,P Knapsack weight: 7 kg Knapsack amount: £14 ./ch-2.pl 2 -Optimal combination: G,P -Knapsack weight: 6 kg +Optimal combination: B,P +Knapsack weight: 5 kg +Knapsack amount: £12 + + +By setting REPEAT_BOX_LIMIT => 3 and calling +./ch-2.pl 7 +Optimal combination: B,B,B,P,P,P +Knapsack weight: 15 kg +Knapsack amount: £36 |
