aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-112/feng-chang/raku/ch-1.raku23
-rwxr-xr-xchallenge-112/feng-chang/raku/ch-2.raku15
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-112/feng-chang/raku/ch-1.raku b/challenge-112/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..cf4cf08e00
--- /dev/null
+++ b/challenge-112/feng-chang/raku/ch-1.raku
@@ -0,0 +1,23 @@
+#!/bin/env raku
+
+sub MAIN(Str:D $path where * ~~ rx{^'/'}) {
+ my @dirs = $path.split(rx{'/'+}).tail(*-1); # put all directory names into an array, omit first empty element
+ @dirs.pop unless @dirs[*-1]; # omit last element if it is empty
+ my $i = 0;
+ try {
+ while $i < @dirs.elems {
+ given @dirs[$i] {
+ when '.' { @dirs.splice($i, 1) } # omit '.'
+ when '..' { @dirs.splice($i-1, 2); --$i; } # omit '..' and its upper directory
+ default { ++$i }
+ }
+ }
+
+ CATCH {
+ $*ERR.put: 'cannot walk beyond root'; # guard against excessive '..'s
+ exit;
+ }
+ }
+
+ put '/', @dirs.join('/');
+}
diff --git a/challenge-112/feng-chang/raku/ch-2.raku b/challenge-112/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..b674d7b518
--- /dev/null
+++ b/challenge-112/feng-chang/raku/ch-2.raku
@@ -0,0 +1,15 @@
+#!/bin/env raku
+
+proto walk(UInt:D --> UInt:D) {*} # restrict dispatch pattern
+
+multi walk(0) { 1 }
+multi walk(1) { 1 }
+multi walk(2) { 2 }
+
+multi walk($n) {
+ state @memo; # use memory to speed up counting
+ @memo[$n] = walk($n-1) + walk($n-2) unless @memo[$n].defined;
+ @memo[$n];
+}
+
+sub MAIN(UInt:D $n) { put walk($n) }