aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Krňávek <Jan.Krnavek@gmail.com>2023-04-23 22:20:19 +0200
committerJan Krňávek <Jan.Krnavek@gmail.com>2023-04-23 22:20:19 +0200
commitb6227b6cf3b0f0860a85211bbfa23f64d9ce0905 (patch)
tree21bf2ffeedc26f826a222f446bf365fbc9681a89
parent2f51a08d594088ee8dd69d0f8dcd289ccf0975c6 (diff)
downloadperlweeklychallenge-club-b6227b6cf3b0f0860a85211bbfa23f64d9ce0905.tar.gz
perlweeklychallenge-club-b6227b6cf3b0f0860a85211bbfa23f64d9ce0905.tar.bz2
perlweeklychallenge-club-b6227b6cf3b0f0860a85211bbfa23f64d9ce0905.zip
solutions week 213
-rw-r--r--challenge-213/wambash/raku/ch-1.raku17
-rw-r--r--challenge-213/wambash/raku/ch-2.raku43
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-213/wambash/raku/ch-1.raku b/challenge-213/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..362d391bd1
--- /dev/null
+++ b/challenge-213/wambash/raku/ch-1.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/env raku
+
+sub fun-sort (+@list) {
+ @list.sort: {$_ % 2 => $_}
+}
+
+multi MAIN (Bool :test($)!) {
+ use Test;
+ is fun-sort(1..6),(2,4,6,1,3,5);
+ is fun-sort(1,2),(2,1);
+ is fun-sort(1),(1);
+ done-testing;
+}
+
+multi MAIN (*@list) {
+ say fun-sort @list
+}
diff --git a/challenge-213/wambash/raku/ch-2.raku b/challenge-213/wambash/raku/ch-2.raku
new file mode 100644
index 0000000000..251f6cf38a
--- /dev/null
+++ b/challenge-213/wambash/raku/ch-2.raku
@@ -0,0 +1,43 @@
+#!/usr/bin/env raku
+
+use v6.e.*;
+
+sub make-graph (+@routes) {
+ @routes
+ andthen |$_,|.map: *.reverse
+ andthen .map: |*.rotor: 2 => -1
+ andthen .classify: *.[0], as => *.[1]
+}
+
+sub step-depth (+@route, :%graph) {
+ %graph{@route.tail}
+ andthen .grep: * ∉ @route
+ andthen .map: { |@route, $_ }\
+ andthen .Slip
+}
+
+
+sub shortest-route (+@routes,:$source!,:$destination!) {
+ my %graph = make-graph @routes;
+ (($source,),), { .map: &step-depth.assuming: :%graph } ... :!elems
+ andthen .map: *.cache.grep: *.tail eqv $destination
+ andthen .first: *.elems
+}
+
+multi MAIN (Bool :test($)!) {
+ use Test;
+ my %graph := make-graph( (1,2,3,5), (2, 4) );
+ is %graph.gist, :{1 => [2], 2 => [3,4,1], 3 => [5,2], 4 => [2], 5 => [3]}.gist;
+ is step-depth( 1,2,3, :%graph), (1,2,3,5);
+ is step-depth( 1, :%graph), (1,2);
+ is step-depth( 2,1, :%graph).elems, 0;
+ is-deeply step-depth( 1,2, :%graph), slip((1,2,3),(1,2,4));
+ is shortest-route((1,2,6),(5,6,7),:1source,:7destination),(1,2,6,7);
+ is shortest-route((1,2,3),(4,5,6,7),:1source,:7destination),Nil;
+ is shortest-route([1,2,3], [4,5,6], [3,8,9], [7,8], :1source,:7destination), (1,2,3,8,7);
+ done-testing;
+}
+
+multi MAIN (*@routes,Str :s(:$source)!,Str :d(:$destination)!) {
+ say shortest-route |@routes.map( *.split(',').cache ), :source($source.Str), :destination($destination.Str)
+}