From 96f32a6c4c7b023cbc8117c768ad0af573cc76bc Mon Sep 17 00:00:00 2001 From: Scimon Date: Tue, 26 Aug 2025 10:42:04 +0100 Subject: PArt 2 complete --- challenge-336/simon-proctor/raku/ch-2.raku | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) mode change 100644 => 100755 challenge-336/simon-proctor/raku/ch-2.raku diff --git a/challenge-336/simon-proctor/raku/ch-2.raku b/challenge-336/simon-proctor/raku/ch-2.raku old mode 100644 new mode 100755 index 41448bbc4d..dbfd8eb218 --- 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; } -- cgit