aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-05-10 18:33:06 +0800
committer冯昶 <seaker@qq.com>2021-05-10 18:33:06 +0800
commit3ba59e2c4ac168b4a6983cb22300568b9b73fac7 (patch)
tree15686806f9408b617bd72ba090874f42c0aa4233
parent7c3e4b829100a613a3dc05c4a378c351f2bfcced (diff)
downloadperlweeklychallenge-club-3ba59e2c4ac168b4a6983cb22300568b9b73fac7.tar.gz
perlweeklychallenge-club-3ba59e2c4ac168b4a6983cb22300568b9b73fac7.tar.bz2
perlweeklychallenge-club-3ba59e2c4ac168b4a6983cb22300568b9b73fac7.zip
challenge 112, raku solutions
-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..5175350730
--- /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);
+ @dirs.pop unless @dirs[*-1];
+ my $i = 0;
+ try {
+ while $i < @dirs.elems {
+ given @dirs[$i] {
+ when '.' { @dirs.splice($i, 1) }
+ when '..' { @dirs.splice($i-1, 2); --$i; }
+ default { ++$i }
+ }
+ }
+
+ CATCH {
+ $*ERR.put: 'cannot walk beyond root';
+ 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..f0ea1a093b
--- /dev/null
+++ b/challenge-112/feng-chang/raku/ch-2.raku
@@ -0,0 +1,15 @@
+#!/bin/env raku
+
+proto walk(UInt:D --> UInt:D) {*}
+
+multi walk(0) { 1 }
+multi walk(1) { 1 }
+multi walk(2) { 2 }
+
+multi walk($n) {
+ state @memo;
+ @memo[$n] = walk($n-1) + walk($n-2) unless @memo[$n].defined;
+ @memo[$n];
+}
+
+sub MAIN(UInt:D $n) { put walk($n) }