aboutsummaryrefslogtreecommitdiff
path: root/challenge-093/feng-chang/ch-2.raku
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-093/feng-chang/ch-2.raku')
-rwxr-xr-xchallenge-093/feng-chang/ch-2.raku24
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-093/feng-chang/ch-2.raku b/challenge-093/feng-chang/ch-2.raku
new file mode 100755
index 0000000000..ff099926a8
--- /dev/null
+++ b/challenge-093/feng-chang/ch-2.raku
@@ -0,0 +1,24 @@
+#!/bin/env raku
+
+sub traverse(Any:D $node, UInt:D $sum --> UInt:D) {
+ $node.^name ~~ 'Int' ??
+ $node + $sum !!
+ [+] (traverse($_, $sum + $node.key) for |$node.value);
+}
+
+multi MAIN('test') {
+ use Test;
+
+ is traverse(1, 0), 1, '1 : 1';
+ is traverse(1 => 2, 0), 3, '1 => 2 : 3';
+ is traverse(1 => (2, 3), 0), 7, '1 => (2, 3) : 7';
+}
+
+my Array $T .= new(
+ 1 => (2 => (3, 4)),
+ 1 => (2 => 4, 3 => (5, 6)),
+);
+
+multi MAIN(UInt:D \n) {
+ put traverse($T[n], 0);
+}