aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-214/mark-anderson/raku/ch-1.raku18
-rw-r--r--challenge-214/mark-anderson/raku/ch-2.raku36
2 files changed, 54 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..fe4c92d8e4
--- /dev/null
+++ b/challenge-214/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,18 @@
+#!/usr/bin/env raku
+use Test;
+
+is rank-score(<1 2 4 3 5>), <5 4 S B G>;
+is rank-score(<8 5 6 7 4>), <G 4 B S 5>;
+is rank-score(<3 5 4 2>), <B G S 4>;
+is rank-score(<2 5 2 1 7 5 1>), <4 S 4 6 G S 6>;
+is rank-score(<20 76 15 55 76 15 20 20 15 20 20>), <4 G 9 B G 9 4 4 9 4 4>;
+
+sub rank-score($list)
+{
+ my $b = $list.Bag;
+ my @a = [\+] flat 1, $b.sort(-*.key)>>.value;
+ @a .= map({ <Nil G S B>[$_] or $_ });
+
+ my %h = $b.keys.sort(-*) Z=> @a;
+ $list.map({ %h{$_} })
+}
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..e24dd8ebf8
--- /dev/null
+++ b/challenge-214/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,36 @@
+#!/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(<1 1 1 25 3 25 3 25 44 9 25 44 25 5 25 5 25 1 1 1>), 92;
+
+sub collect-points(*@a)
+{
+ sum gather while @a
+ {
+ my $c = @a.pairs.classify({ .value }, :as{ .key });
+ my $p = $c.first({ consecutive(.value) });
+
+ if $p
+ {
+ take $p.value.elems ** 2
+ }
+
+ else
+ {
+ $p = $c.Bag.minpairs.head;
+ take $p.value
+ }
+
+ @a[ $c{$p.key} ]:eject
+ }
+}
+
+sub consecutive(@a)
+{
+ .tail - .head == .end and [<] $_ given @a
+}