aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-23 17:45:21 +0100
committerGitHub <noreply@github.com>2023-05-23 17:45:21 +0100
commit0597e8c36a08ec74176d68d3832afe40932dd6ae (patch)
treed0885a6807b1690da5c55af3e81acd4e7fd0dd92
parentc8670df894db150b6176b9128224122acf9a4a52 (diff)
parente98205d29db7fb9d908066caab438faee979f8ce (diff)
downloadperlweeklychallenge-club-0597e8c36a08ec74176d68d3832afe40932dd6ae.tar.gz
perlweeklychallenge-club-0597e8c36a08ec74176d68d3832afe40932dd6ae.tar.bz2
perlweeklychallenge-club-0597e8c36a08ec74176d68d3832afe40932dd6ae.zip
Merge pull request #8122 from andemark/challenge-218-Raku
Challenge 218 Solutions (Raku)
-rw-r--r--challenge-218/mark-anderson/raku/ch-1.raku29
-rw-r--r--challenge-218/mark-anderson/raku/ch-2.raku127
2 files changed, 156 insertions, 0 deletions
diff --git a/challenge-218/mark-anderson/raku/ch-1.raku b/challenge-218/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..0a3604f3b9
--- /dev/null
+++ b/challenge-218/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,29 @@
+#!/usr/bin/env raku
+use Test;
+
+is max-product(3,1,2), 6;
+is max-product(4,1,3,2), 24;
+is max-product(-1,0,1,3,1), 3;
+is max-product(-8,2,-9,0,-4,3), 216;
+is max-product(-8,-9,0,0,0,1), 72;
+is max-product(1,-2,3,4,5), 60;
+is max-product(1,-2,3), -6;
+is max-product(4,0,-3,5), 0;
+is max-product(-3,-2,0,-4), 0;
+is max-product(-3,-2,1,-4), 12;
+is max-product(-3,-5,-2,-4), -24;
+is max-product(-1,-1,1,1), 1;
+is max-product(-1,-1,-1,0), 0;
+is max-product(-1,-1,-1,1), 1;
+is max-product(1,1,1,0), 1;
+is max-product(0,0,0,0), 0;
+is max-product(1,2,0,-1), 0;
+is max-product(-1,0,0,-1), 0;
+
+sub max-product(+$a)
+{
+ given $a.sort.List
+ {
+ max ([*] .tail(3)), ([*] flat .head(2), .tail)
+ }
+}
diff --git a/challenge-218/mark-anderson/raku/ch-2.raku b/challenge-218/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..f6c6d20441
--- /dev/null
+++ b/challenge-218/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,127 @@
+#!/usr/bin/env raku
+use Test;
+
+is matrix-score([0,0,1,1],
+ [1,0,1,0],
+ [1,1,0,0]), 39;
+
+is matrix-score([0]), 1;
+
+is matrix-score([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
+ [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
+ [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
+ [0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0],
+ [0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0],
+ [0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0],
+ [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0],
+ [0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0],
+ [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
+ [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
+ [0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0],
+ [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0],
+ [0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0],
+ [0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0],
+ [0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0],
+ [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
+ [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
+ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]), 2246857;
+
+is matrix-score([0,0,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,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
+ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]), 32212303841;
+
+sub matrix-score(+@matrix)
+{
+ $_ = .map(+not *).Array unless .[0] for @matrix;
+
+ @matrix = ([Z] @matrix)>>.Array.Array;
+
+ for @matrix.skip
+ {
+ my %b = .Bag;
+ $_ = .map(+not *).Array if (%b<0> // 0) > (%b<1> // 0)
+ }
+
+ [+] ([Z] @matrix).map({ .join.parse-base(2) })
+}