aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-05 13:03:03 +0100
committerGitHub <noreply@github.com>2024-08-05 13:03:03 +0100
commit421e5759d57ff15670dbc4438a1b2ff4f9e6092d (patch)
tree2b0b2e289a80fb9220a40ee0445052610410460d
parentd4691a9d293de4cf2f42e44cdefabb0b455045fb (diff)
parentc448177a97d3340aeec03e47b6b9d5f6795b0d7e (diff)
downloadperlweeklychallenge-club-421e5759d57ff15670dbc4438a1b2ff4f9e6092d.tar.gz
perlweeklychallenge-club-421e5759d57ff15670dbc4438a1b2ff4f9e6092d.tar.bz2
perlweeklychallenge-club-421e5759d57ff15670dbc4438a1b2ff4f9e6092d.zip
Merge pull request #10543 from andemark/challenge-281
Challenge 281 Solutions (Raku)
-rw-r--r--challenge-281/mark-anderson/raku/ch-1.raku14
-rw-r--r--challenge-281/mark-anderson/raku/ch-2.raku48
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-281/mark-anderson/raku/ch-1.raku b/challenge-281/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..cc2e15df8e
--- /dev/null
+++ b/challenge-281/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,14 @@
+#!/usr/bin/env raku
+use Test;
+
+ok check-color('d3');
+nok check-color('g5');
+ok check-color('e6');
+
+sub check-color($coordinates)
+{
+ if $coordinates ~~ /(<.alpha>)(<.digit>)/ -> ($col, $row)
+ {
+ one($col.ord, $row) %% 2 # XOR
+ }
+}
diff --git a/challenge-281/mark-anderson/raku/ch-2.raku b/challenge-281/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..e564a4e37e
--- /dev/null
+++ b/challenge-281/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,48 @@
+#!/usr/bin/env raku
+use Test;
+
+# With help from https://www.geeksforgeeks.org/minimum-steps-reach-target-knight/
+
+is knights-move('g2', 'a8'), 4;
+is knights-move('g2', 'h2'), 3;
+
+sub knights-move($start is copy, $end is copy)
+{
+ $start .= &coordinates;
+ $end .= &coordinates;
+
+ my $mv := (-2,-1),(-1,-2),(1,-2),(2,-1),(-2,1),(-1,2),(1,2),(2,1);
+
+ my @queue;
+ my @visit;
+
+ @queue.push: { :xy($start), :dis(0) }
+ @visit[$start[0];$start[1]] = True;
+
+ while @queue
+ {
+ my %t = shift @queue;
+ return %t<dis> if %t<xy> eqv $end;
+
+ for $mv
+ {
+ my $xy := %t<xy> >>+<< $_;
+
+ if all(all($xy) ~~ ^8, not @visit[$xy[0];$xy[1]])
+ {
+ @visit[$xy[0];$xy[1]] = True;
+ @queue.push: { :$xy, :dis(%t<dis> + 1) }
+ }
+ }
+ }
+
+ return Inf
+}
+
+sub coordinates($str)
+{
+ if $str ~~ /(<.alpha>)(<.digit>)/ -> ($col, $row)
+ {
+ 8 - $row, $col.ord - 97
+ }
+}