aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2023-05-22 12:20:10 +0000
committerMark <53903062+andemark@users.noreply.github.com>2023-05-22 12:20:10 +0000
commit02ef15bd7e68e35a558c767dacf1be6afb818581 (patch)
tree2dc554084a1b64ed30a088e1a9717c2002180354
parentc8670df894db150b6176b9128224122acf9a4a52 (diff)
downloadperlweeklychallenge-club-02ef15bd7e68e35a558c767dacf1be6afb818581.tar.gz
perlweeklychallenge-club-02ef15bd7e68e35a558c767dacf1be6afb818581.tar.bz2
perlweeklychallenge-club-02ef15bd7e68e35a558c767dacf1be6afb818581.zip
Challenge 218 Solutions (Raku)
-rw-r--r--challenge-218/mark-anderson/raku/ch-1.raku40
-rw-r--r--challenge-218/mark-anderson/raku/ch-2.raku43
2 files changed, 83 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..3c1936be9b
--- /dev/null
+++ b/challenge-218/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,40 @@
+#!/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(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;
+
+multi max-product(+$a where .elems == 3)
+{
+ [*] |$a
+}
+
+multi max-product(+$a)
+{
+ my %c = $a.classify({ .sign });
+
+ return [*] $a.sort.tail(3) if $a == any %c.values;
+
+ my @neg = %c{-1}.sort.head(2);
+ my @pos = %c{1}.sort.tail(3);
+
+ max gather
+ {
+ take 0;
+ take [*] @pos if @pos == 3;
+ take [*] |@neg, @pos.max if @neg == 2
+ }
+}
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..3a3248057e
--- /dev/null
+++ b/challenge-218/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,43 @@
+#!/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;
+
+sub matrix-score(+@matrix)
+{
+ $_ = .map(* +^ 1).Array unless .[0] for @matrix;
+
+ @matrix = ([Z] @matrix)>>.Array.Array;
+
+ for @matrix.skip
+ {
+ my %b = .Bag;
+ $_ = .map(* +^ 1).Array if (%b<0> // 0) > (%b<1> // 0)
+ }
+
+ [+] ([Z] @matrix).map({ .join.parse-base(2) })
+}