aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Anderson <mark@andemark.io>2024-08-05 03:35:15 +0000
committerMark Anderson <mark@andemark.io>2024-08-05 03:35:15 +0000
commitaa85899a858075bfd9275244a84cdce81a794b38 (patch)
tree339c65a3f626e167ef0dbe31ae4ee40a170b0df0
parentd4691a9d293de4cf2f42e44cdefabb0b455045fb (diff)
downloadperlweeklychallenge-club-aa85899a858075bfd9275244a84cdce81a794b38.tar.gz
perlweeklychallenge-club-aa85899a858075bfd9275244a84cdce81a794b38.tar.bz2
perlweeklychallenge-club-aa85899a858075bfd9275244a84cdce81a794b38.zip
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.raku51
2 files changed, 65 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..a5cc293e19
--- /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($arg)
+{
+ if $arg ~~ /(<.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..06ad295d0f
--- /dev/null
+++ b/challenge-281/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,51 @@
+#!/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($begin is copy, $end is copy)
+{
+ $begin .= &coordinates;
+ $end .= &coordinates;
+
+ my $dx := -2, -1, 1, 2, -2, -1, 1, 2;
+ my $dy := -1, -2, -2, -1, 1, 2, 2, 1;
+
+ my @queue;
+ my @visit;
+
+ @queue.push: { :x($begin[0]), :y($begin[1]), :dis(0) }
+ @visit[$begin[0];$begin[1]] = True;
+
+ while @queue
+ {
+ my %t = shift @queue;
+
+ return %t<dis> if all(%t<x> == $end[0], %t<y> == $end[1]);
+
+ for ^8
+ {
+ my $x = %t<x> + $dx[$_];
+ my $y = %t<y> + $dy[$_];
+
+ if all(all($x,$y) ~~ 0..7, not @visit[$x;$y])
+ {
+ @visit[$x;$y] = True;
+ @queue.push: { :$x, :$y, :dis(%t<dis> + 1) }
+ }
+ }
+ }
+
+ return Inf
+}
+
+sub coordinates($str)
+{
+ if $str ~~ /(<.alpha>)(<.digit>)/ -> ($col, $row)
+ {
+ 8 - $row, $col.ord - 97
+ }
+}