diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-27 16:41:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-27 16:41:57 +0100 |
| commit | f1e76b84d182ad09fe83832ce8a598a7d4aa77ee (patch) | |
| tree | 7eb16ab2c2632902f71397c5677069ed2b8c2061 | |
| parent | c2faea053751fb6a48e1ddaab1476ccac3336376 (diff) | |
| parent | 1294e29fc71a9b3887b38a3650f3f274ff2464f7 (diff) | |
| download | perlweeklychallenge-club-f1e76b84d182ad09fe83832ce8a598a7d4aa77ee.tar.gz perlweeklychallenge-club-f1e76b84d182ad09fe83832ce8a598a7d4aa77ee.tar.bz2 perlweeklychallenge-club-f1e76b84d182ad09fe83832ce8a598a7d4aa77ee.zip | |
Merge pull request #7970 from andemark/challenge-214-Raku
ch-1.raku do-over
| -rw-r--r-- | challenge-214/mark-anderson/raku/ch-1.raku | 18 | ||||
| -rw-r--r-- | challenge-214/mark-anderson/raku/ch-2.raku | 36 |
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 +} |
