aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2023-04-24 07:11:55 +0000
committerMark <53903062+andemark@users.noreply.github.com>2023-04-24 07:11:55 +0000
commiteaaf2ee7172c6b56a0d2cf86fc0d8ea538c6be75 (patch)
tree209768ce5b68978726078327cd368bdf377a9590
parent9df2d961ae00534346eaaceffaf8cfee4ecc88bb (diff)
downloadperlweeklychallenge-club-eaaf2ee7172c6b56a0d2cf86fc0d8ea538c6be75.tar.gz
perlweeklychallenge-club-eaaf2ee7172c6b56a0d2cf86fc0d8ea538c6be75.tar.bz2
perlweeklychallenge-club-eaaf2ee7172c6b56a0d2cf86fc0d8ea538c6be75.zip
Challenge 214 Solutions (Raku)
-rw-r--r--challenge-214/mark-anderson/raku/ch-1.raku16
-rw-r--r--challenge-214/mark-anderson/raku/ch-2.raku27
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..aeb0059f6e
--- /dev/null
+++ b/challenge-214/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,16 @@
+#!/usr/bin/env raku
+use Test;
+
+is-deeply rank-score(<1 2 4 3 5>), <5 4 S B G>>>.Str;
+is-deeply rank-score(<8 5 6 7 4>), <G 4 B S 5>>>.Str;
+is-deeply rank-score(<3 5 4 2>), <B G S 4>>>.Str;
+is-deeply rank-score(<2 5 2 1 7 5 1>), <B S B 4 G S 4>>>.Str;
+is-deeply rank-score(<3 5>), <S G>;
+
+sub rank-score($a)
+{
+ my $u := $a.unique.sort(-*);
+ my $s := <G S B 4 5>...*;
+
+ $a.trans($u => $s.head($u)).comb(/<alnum>/)
+}
diff --git a/challenge-214/mark-anderson/raku/ch-2.raku b/challenge-214/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..fcc690a760
--- /dev/null
+++ b/challenge-214/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,27 @@
+#!/usr/bin/env raku
+use Adverb::Eject;
+use Test;
+
+is collect-points(2,4,3,3,3,4,5,4,2), 23;
+is collect-points(1,2,2,2,2,1), 20;
+is collect-points(1), 1;
+is collect-points(2,2,2,1,1,2,2,2), 40;
+is collect-points(9,1,1,1,35,35,44,1,5,5,2,5,2,5,44,85,7,7,7,85,1,1,1), 91;
+
+sub collect-points(*@a)
+{
+ sum gather while @a
+ {
+ my $c = @a.pairs.classify({ .value }, :as{ .key });
+ my $p = $c.first({ consecutive(.value) });
+ my $a = $p ?? $p.value !! $c{ @a.Bag.minpairs.head.key };
+
+ @a[ $a ]:eject;
+ take $a.elems ** 2
+ }
+}
+
+sub consecutive(@a)
+{
+ .tail - .head == .end and [<] $_ given @a
+}