aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x[-rw-r--r--]challenge-336/simon-proctor/raku/ch-2.raku29
1 files changed, 28 insertions, 1 deletions
diff --git a/challenge-336/simon-proctor/raku/ch-2.raku b/challenge-336/simon-proctor/raku/ch-2.raku
index 41448bbc4d..dbfd8eb218 100644..100755
--- a/challenge-336/simon-proctor/raku/ch-2.raku
+++ b/challenge-336/simon-proctor/raku/ch-2.raku
@@ -8,5 +8,32 @@ multi sub MAIN(:t(:$test)) is hidden-from-USAGE {
is final-score("-5","-10","+","D","C","+"), -55;
is final-score("3","6","+","D","C","8","+","D","-2","C","+"), 128;
done-testing;
-
+}
+
+subset ValidScore of Str where /^ "-"?\d+ || "D" || "C" || "+" $/;
+
+multi sub MAIN(*@scores where all(@scores) ~~ ValidScore ) {
+ final-score(|@scores).say;
+}
+
+sub final-score(*@scores where all(@scores) ~~ ValidScore ) {
+ my @results = [];
+ for @scores -> $score {
+ given $score {
+ when ( 'C' ) {
+ @results.pop;
+ succeed;
+ }
+ when ( "D") {
+ @results.push( @results[*-1] * 2);
+ succeed;
+ }
+ when ("+") {
+ @results.push( @results[*-1] + @results[*-2] );
+ succeed;
+ }
+ default { @results.push($score.Int) }
+ }
+ }
+ return [+] @results;
}