aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Smith <aaronreidsmith@gmail.com>2021-05-15 19:50:04 -0500
committerAaron Smith <aaronreidsmith@gmail.com>2021-05-15 19:50:04 -0500
commitf3fede6d074f588a630fbfe141c2aa96e55563f6 (patch)
tree1a38f355eca2c67268211e29aa0c178aaf4e349b
parentdb79192d2c62c4680c6fda0f0ef3b65c41581e01 (diff)
downloadperlweeklychallenge-club-f3fede6d074f588a630fbfe141c2aa96e55563f6.tar.gz
perlweeklychallenge-club-f3fede6d074f588a630fbfe141c2aa96e55563f6.tar.bz2
perlweeklychallenge-club-f3fede6d074f588a630fbfe141c2aa96e55563f6.zip
Challenge 112 - Raku
-rw-r--r--challenge-112/aaronreidsmith/blog.txt1
-rw-r--r--challenge-112/aaronreidsmith/raku/ch-1.raku43
-rw-r--r--challenge-112/aaronreidsmith/raku/ch-2.raku38
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-112/aaronreidsmith/blog.txt b/challenge-112/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..3a9b06e61b
--- /dev/null
+++ b/challenge-112/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-112/
diff --git a/challenge-112/aaronreidsmith/raku/ch-1.raku b/challenge-112/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..748081b826
--- /dev/null
+++ b/challenge-112/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,43 @@
+#!/usr/bin/env raku
+
+sub challenge(Str $path is copy) returns Str {
+ die "Must be an absolute path" unless $path.starts-with('/');
+
+ $path = $path.substr(1);
+ $path = $path.subst(/\/ ** 2..*/, '/', :global);
+ $path = $path.substr(0, *-1) if $path.ends-with('/');
+
+ my @output;
+ for $path.split('/') -> $dir {
+ given $dir {
+ when '.' { Nil }
+ when '..' {
+ die "Illegal path" if @output.elems == 0;
+ @output.pop;
+ }
+ default { @output.push($dir) }
+ }
+ }
+ '/' ~ @output.join('/')
+}
+
+multi sub MAIN(Str $path) {
+ say challenge($path);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ ('/a/', '/a'),
+ ('/a/b//c/', '/a/b/c'),
+ ('/a/b/c/../../', '/a'),
+ ('/a/b/./c/./d/', '/a/b/c/d')
+ );
+
+ for @tests -> ($input, $expected) {
+ is(challenge($input), $expected);
+ }
+
+ done-testing;
+}
diff --git a/challenge-112/aaronreidsmith/raku/ch-2.raku b/challenge-112/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..4ef27f4cbf
--- /dev/null
+++ b/challenge-112/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+
+use experimental :cached;
+
+sub challenge(
+ $max where Int,
+ @paths where Positional = (^$max),
+ $current-step where Int = 0
+) is cached {
+ given $current-step {
+ when * == $max { 1 }
+ when * > $max { 0 }
+ default {
+ challenge($max, @paths, $current-step + 1) +
+ challenge($max, @paths, $current-step + 2)
+ }
+ }
+}
+
+multi sub MAIN(Int $n) {
+ say challenge($n);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (3, 3),
+ (4, 5),
+ (10, 89)
+ );
+
+ for @tests -> ($n, $expected) {
+ is(challenge($n), $expected);
+ }
+
+ done-testing;
+}