aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-214/mark-anderson/raku/ch-2.raku36
1 files changed, 36 insertions, 0 deletions
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..0908191003
--- /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
+ {
+ @a[ $c{$p.key} ]:eject
+ }
+
+ else
+ {
+ $p = $c.classify({ .key }, :as{ .value.elems }).minpairs.head;
+ @a[ $c{$p.key}.head ]:eject
+ }
+
+ take $p.value.elems ** 2
+ }
+}
+
+sub consecutive(@a)
+{
+ .tail - .head == .end and [<] $_ given @a
+}