aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Anderson <mark@frontrangerunner.com>2019-11-30 09:54:39 -0700
committerMark Anderson <mark@frontrangerunner.com>2019-11-30 09:54:39 -0700
commit8e48385faa5f88d1d63eb0b05cc8af82562581f3 (patch)
tree6f0ebe1930386425a63ce8b2e4fd5712e07acb56
parentdb4142b11f4a49fcce2041e367ddd47c13e37e62 (diff)
downloadperlweeklychallenge-club-8e48385faa5f88d1d63eb0b05cc8af82562581f3.tar.gz
perlweeklychallenge-club-8e48385faa5f88d1d63eb0b05cc8af82562581f3.tar.bz2
perlweeklychallenge-club-8e48385faa5f88d1d63eb0b05cc8af82562581f3.zip
rewrote ch-2.pl
-rw-r--r--challenge-036/mark-anderson/perl5/ch-2.pl55
1 files changed, 26 insertions, 29 deletions
diff --git a/challenge-036/mark-anderson/perl5/ch-2.pl b/challenge-036/mark-anderson/perl5/ch-2.pl
index 4f987df09e..6529d3075a 100644
--- a/challenge-036/mark-anderson/perl5/ch-2.pl
+++ b/challenge-036/mark-anderson/perl5/ch-2.pl
@@ -3,46 +3,43 @@ use Modern::Perl '2018';
use List::Util 'sum';
use Algorithm::Combinatorics 'combinations';
-my @boxes = (
- { Color => 'R', Weight => 1, Amount => 1 },
- { Color => 'B', Weight => 1, Amount => 2 },
- { Color => 'G', Weight => 2, Amount => 2 },
- { Color => 'Y', Weight => 12, Amount => 4 },
- { Color => 'P', Weight => 4, Amount => 10 }
-);
-
+my @indices = (0 .. 4);
+my @colors = qw/R B G Y P/;
+my @weights = (1, 1, 2, 12, 4);
+my @amounts = (1, 2, 2, 4, 10);
my @answers;
-foreach my $k (1 .. 5) {
- my $biggest_sum = 0;
- my @combinations = combinations(\@boxes, $k);
+foreach my $k (@indices) {
+ my $current_max_amount = 0;
+ my @combinations = combinations(\@indices, $k+1);
foreach my $combination (@combinations) {
- my @colors = map { $_->{Color} } $combination->@*;
- my @weights = map { $_->{Weight} } $combination->@*;
- my @amounts = map { $_->{Amount} } $combination->@*;
-
- my $weight_sum = sum @weights;
- my $amount_sum = sum @amounts;
+ my $colors = join q{}, @colors[$combination->@*];
+ my $weight_sum = sum @weights[$combination->@*];
+ my $amount_sum = sum @amounts[$combination->@*];
- if($weight_sum <= 15 and $amount_sum > $biggest_sum) {
- $biggest_sum = $amount_sum;
- $answers[$k-1] = [$k, (join '', @colors), $weight_sum, $amount_sum];
+ if($weight_sum <= 15 and $amount_sum > $current_max_amount) {
+ $current_max_amount = $amount_sum;
+ $answers[$k] = [ $k+1, $colors, $weight_sum, $amount_sum ];
}
}
}
-say "-" x 42 . "\nNumber Of Boxes | Colors | Weight | Amount\n" . "-" x 42;
+print <<~HEADER;
+ --------------------------------
+ Boxes | Colors | Weight | Amount
+ --------------------------------
+ HEADER
foreach my $answer (@answers) {
- printf "%-18d%-9s%-9d%d\n", $answer->@*;
+ printf "%-8d%-9s%-9d%d\n", $answer->@*;
}
# Output:
-# ------------------------------------------
-# Number Of Boxes | Colors | Weight | Amount
-# ------------------------------------------
-# 1 P 4 10
-# 2 BP 5 12
-# 3 BGP 7 14
-# 4 RBGP 8 15
+# --------------------------------
+# Boxes | Colors | Weight | Amount
+# --------------------------------
+# 1 P 4 10
+# 2 BP 5 12
+# 3 BGP 7 14
+# 4 RBGP 8 15