aboutsummaryrefslogtreecommitdiff
path: root/challenge-213
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-18 18:59:25 +0100
committerGitHub <noreply@github.com>2023-04-18 18:59:25 +0100
commitde2186a2d7c9f9a7d7732abf8ed6eed63dfc641c (patch)
tree59f6c6ece3e5b6edfb4adc492abad34cabf56bed /challenge-213
parent42228ce601fe37769faacc051f35f74b9566bb26 (diff)
parent83530ce6d5410f67279d189e0167270561a436fc (diff)
downloadperlweeklychallenge-club-de2186a2d7c9f9a7d7732abf8ed6eed63dfc641c.tar.gz
perlweeklychallenge-club-de2186a2d7c9f9a7d7732abf8ed6eed63dfc641c.tar.bz2
perlweeklychallenge-club-de2186a2d7c9f9a7d7732abf8ed6eed63dfc641c.zip
Merge pull request #7921 from andemark/challenge-213-Raku
Initial 213 (Raku)
Diffstat (limited to 'challenge-213')
-rw-r--r--challenge-213/mark-anderson/raku/ch-1.raku13
-rw-r--r--challenge-213/mark-anderson/raku/ch-2.raku61
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-213/mark-anderson/raku/ch-1.raku b/challenge-213/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..d3a64916d2
--- /dev/null
+++ b/challenge-213/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,13 @@
+#!/usr/bin/env raku
+use Test;
+
+is-deeply fun-sort(1,2,3,4,5,6), (2,4,6,1,3,5);
+is-deeply fun-sort(1,2), (2,1);
+is-deeply fun-sort(1), (1,);
+is-deeply fun-sort(1,2,3,4,5,6,4,5,6), (2,4,4,6,6,1,3,5,5);
+
+sub fun-sort(*@a)
+{
+ my %c = @a.classify({ $_ %% 2 ?? 'even' !! 'odd' });
+ flat (%c<even> || Empty, %c<odd> || Empty)>>.sort
+}
diff --git a/challenge-213/mark-anderson/raku/ch-2.raku b/challenge-213/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..2aeb6a2b4a
--- /dev/null
+++ b/challenge-213/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,61 @@
+#!/usr/bin/env raku
+use Test;
+
+is-deeply shortest-path(((1,2,6), (5,6,7)), 1,7), (1,2,6,7);
+is-deeply shortest-path(((1,2,3), (4,5,6)), 2,5), -1;
+is-deeply shortest-path(((1,2,3), (4,5,6), (3,8,9), (7,8)), 1,7), (1,2,3,8,7);
+
+# The graph from https://www.youtube.com/watch?v=T_m27bhVQQQ&t=360s
+is-deeply shortest-path((<A C F>, <A G J>, <A B D>,
+ <B A C>, <B D H>,
+ <C A G>, <C F J>, <C G J>,
+ <D B A>, <D G J>, <D H J>,
+ <F C G>, <F J H>,
+ <G A B>, <G C A>, <G D H>, <G J F>,
+ <H D G>, <H J G>,
+ <J F C>, <J G A>, <J H D>), 'A','H'), any(<A G J H>,
+ <A B D H>,
+ <A G D H>);
+
+sub shortest-path($routes, $source, $destination is copy)
+{
+ my %graph = graph($routes);
+ my $from = $source;
+ my @queue;
+ my %path;
+
+ until %path{$destination}
+ {
+ @queue.push: $from => $_ unless %path{$_} for %graph{$from};
+ return -1 unless @queue;
+ %path{.value} = .key unless %path{.value} given @queue.head;
+ $from = @queue.shift.value
+ }
+
+ reverse gather until $destination ~~ $source
+ {
+ take $destination;
+ $destination = %path{$destination};
+ LAST { take $source }
+ }
+}
+
+sub graph($routes)
+{
+ my %graph;
+
+ for |$routes -> $r
+ {
+ %graph{$r[0]}.push: $r[1];
+
+ for 1..$r.end.pred
+ {
+ %graph{$r[$_]}.push: $r[.pred];
+ %graph{$r[$_]}.push: $r[.succ]
+ }
+
+ %graph{$r[$r.end]}.push: $r[$r.end.pred]
+ }
+
+ %graph>>.values>>.unique>>.Slip
+}