aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-336/ash/raku/ch-2.raku23
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-336/ash/raku/ch-2.raku b/challenge-336/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..2758b8edcb
--- /dev/null
+++ b/challenge-336/ash/raku/ch-2.raku
@@ -0,0 +1,23 @@
+# Task 2 of the Weekly Challenge 336
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-336/#TASK2
+
+say score "5", "2", "C", "D", "+"; # 30
+say score "5", "-2", "4", "C", "D", "9", "+", "+"; # 27
+say score "7", "D", "D", "C", "+", "3"; # 45
+say score "-5", "-10", "+", "D", "C", "+"; # -55
+say score "3", "6", "+", "D", "C", "8", "+", "D", "-2", "C", "+"; # 128
+
+sub score(*@list) {
+ my @stack;
+
+ for @list -> $item {
+ given $item {
+ when /\d/ {@stack.push($item)}
+ when 'C' {@stack.pop if @stack}
+ when 'D' {@stack.push(@stack[*-1] * 2) if @stack}
+ when '+' {@stack.push((@stack[*-1] // 0) + (@stack[*-2] // 0))}
+ }
+ }
+
+ return [+] @stack;
+}