aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-036/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-036/jaldhar-h-vyas/perl/ch-2.pl55
-rwxr-xr-xchallenge-036/jaldhar-h-vyas/raku/ch-2.p633
3 files changed, 89 insertions, 0 deletions
diff --git a/challenge-036/jaldhar-h-vyas/blog.txt b/challenge-036/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..9f61ec6eb9
--- /dev/null
+++ b/challenge-036/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2019/12/perl_weekly_challenge_week_36.html
diff --git a/challenge-036/jaldhar-h-vyas/perl/ch-2.pl b/challenge-036/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..011a3876ab
--- /dev/null
+++ b/challenge-036/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+my @boxes = (
+ { name => 'R', weight => 1, amount => 1 },
+ { name => 'B', weight => 1, amount => 2 },
+ { name => 'G', weight => 2, amount => 2 },
+ { name => 'Y', weight => 12, amount => 4 },
+ { name => 'P', weight => 4, amount => 10 },
+);
+
+for my $i (2 .. 5) {
+ my $max = { name => q{}, weight => 0, amount => -1 };
+
+ for my $combo (combinations(\@boxes, $i)) {
+ my $total = { name => q{}, weight => 0, amount => 0 };
+ for my $box (@{$combo}) {
+ $total->{name} .= $box->{name};
+ $total->{weight} += $box->{weight};
+ $total->{amount} += $box->{amount};
+ }
+ if ($total->{weight} <= 15 && $total->{amount} > $max->{amount}) {
+ $max = $total;
+ }
+ }
+ if ($max->{amount} > -1) {
+ say "For $i boxes, the best combination is $max->{name} which weighs ",
+ "$max->{weight} kg and is valued £$max->{amount}.";
+ } else {
+ say "for $i boxes, it is not possible to meet the criteria.";
+ }
+} \ No newline at end of file
diff --git a/challenge-036/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-036/jaldhar-h-vyas/raku/ch-2.p6
new file mode 100755
index 0000000000..8e85e110e6
--- /dev/null
+++ b/challenge-036/jaldhar-h-vyas/raku/ch-2.p6
@@ -0,0 +1,33 @@
+#!/usr/bin/perl6
+
+multi sub MAIN() {
+ my @boxes = (
+ { name => 'R', weight => 1, amount => 1 },
+ { name => 'B', weight => 1, amount => 2 },
+ { name => 'G', weight => 2, amount => 2 },
+ { name => 'Y', weight => 12, amount => 4 },
+ { name => 'P', weight => 4, amount => 10 },
+ );
+
+ for 2 .. 5 -> $i {
+ my $max = { name => q{}, weight => 0, amount => -1 };
+
+ for @boxes.combinations($i) -> @combo {
+ my $total = { name => q{}, weight => 0, amount => 0 };
+ for @combo -> $box {
+ $total<name> ~= $box<name>;
+ $total<weight> += $box<weight>;
+ $total<amount> += $box<amount>;
+ }
+ if $total<weight> <= 15 && $total<amount> > $max<amount> {
+ $max = $total;
+ }
+ }
+ if $max<amount> > -1 {
+ say "For $i boxes, the best combination is $max<name> which weighs",
+ " $max<weight> kg and is valued £$max<amount>.";
+ } else {
+ say "for $i boxes, it is not possible to meet the criteria.";
+ }
+ }
+} \ No newline at end of file