aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Shitov <mail@andreyshitov.com>2025-08-29 21:26:12 +0200
committerAndrew Shitov <mail@andreyshitov.com>2025-08-29 21:26:12 +0200
commit69284e2818166b062c9b1462ab7f87c8857c8422 (patch)
tree7dd22a028c7067d50c24bfa84efedb091a4c4c80
parent778a877423d75f6299c57bb75c3edffd2f304e84 (diff)
downloadperlweeklychallenge-club-69284e2818166b062c9b1462ab7f87c8857c8422.tar.gz
perlweeklychallenge-club-69284e2818166b062c9b1462ab7f87c8857c8422.tar.bz2
perlweeklychallenge-club-69284e2818166b062c9b1462ab7f87c8857c8422.zip
Week 336, Task 2, Raku, @ash
-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;
+}