aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarnesom <arne@bbop.org>2024-08-11 19:38:37 +0200
committerarnesom <arne@bbop.org>2024-08-11 19:38:37 +0200
commit4eb8f4e10771b784bb18be1cde0bce8cc534132e (patch)
treea55788778bb0b0ec384696af63daddc3674ee858
parentb7440fc8a1cedf364f75a0a846ee8eb3e63cb158 (diff)
downloadperlweeklychallenge-club-4eb8f4e10771b784bb18be1cde0bce8cc534132e.tar.gz
perlweeklychallenge-club-4eb8f4e10771b784bb18be1cde0bce8cc534132e.tar.bz2
perlweeklychallenge-club-4eb8f4e10771b784bb18be1cde0bce8cc534132e.zip
Arne Sommer
-rw-r--r--challenge-281/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-281/arne-sommer/raku/ch-1.raku13
-rwxr-xr-xchallenge-281/arne-sommer/raku/ch-2.raku44
-rwxr-xr-xchallenge-281/arne-sommer/raku/check-color13
-rwxr-xr-xchallenge-281/arne-sommer/raku/check-color-boring19
-rwxr-xr-xchallenge-281/arne-sommer/raku/knightmover44
6 files changed, 134 insertions, 0 deletions
diff --git a/challenge-281/arne-sommer/blog.txt b/challenge-281/arne-sommer/blog.txt
new file mode 100644
index 0000000000..aa16585683
--- /dev/null
+++ b/challenge-281/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/color-knight.html
diff --git a/challenge-281/arne-sommer/raku/ch-1.raku b/challenge-281/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..e1da604601
--- /dev/null
+++ b/challenge-281/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,13 @@
+#! /usr/bin/env raku
+
+subset BOARDPOS where * ~~ /^<[a..h]><[1..8]>$/;
+
+unit sub MAIN (BOARDPOS $coordinate, :v(:$verbose));
+
+my ($col, $row) = $coordinate.comb;
+
+say ": Row $row, Col $col" if $verbose;
+
+say $col eq any(<a c e g>)
+ ?? $row %% 2
+ !! $row !%% 2;
diff --git a/challenge-281/arne-sommer/raku/ch-2.raku b/challenge-281/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..377739fecb
--- /dev/null
+++ b/challenge-281/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,44 @@
+#! /usr/bin/env raku
+
+subset BOARDPOS where * ~~ /^<[a..h]><[1..8]>$/;
+subset MOVE of Int where -7 <= * <= 7;
+
+unit sub MAIN (BOARDPOS $start, BOARDPOS $end, :v(:$verbose));
+
+my %seen = ( $start => True );
+
+my @queue = \{ steps => 0, pos => $start, path => ($start,) };
+
+while @queue
+{
+ my %curr = @queue.shift;
+
+ say ": Steps %curr<steps>: at pos %curr<pos> (path: %curr<path>)" if $verbose;
+
+ if (%curr<pos> eq $end)
+ {
+ say ": Arrived at target after %curr<steps> steps (path: %curr<path>)" if $verbose;
+ say %curr<steps>;
+ last;
+ }
+
+ for ( (-2,-1), (-2,1), (2,-1), (2,1), (-1,-2), (-1,2), (1,-2), (1,2)) -> ($row-offset, $col-offset)
+ {
+ my $new-pos = new-pos-knight(%curr<pos>, c => $col-offset, r => $row-offset) // next;
+ next if %seen{$new-pos}++;
+ @queue.push: { pos => $new-pos, steps => %curr<steps> +1, path => ( %curr<path>, $new-pos).flat };
+ }
+}
+
+sub new-pos-knight (BOARDPOS $pos, MOVE :r(:$row-offset), MOVE :c(:$col-offset))
+{
+ my ($col, $row) = $pos.comb;
+
+ my $new-row = $row + $row-offset;
+ return unless 1 <= $new-row <= 8;
+
+ my $new-col = (ord($col) + $col-offset).chr;
+ return unless $new-col eq any(<a b c d e f g h>);
+
+ return $new-col ~ $new-row;
+} \ No newline at end of file
diff --git a/challenge-281/arne-sommer/raku/check-color b/challenge-281/arne-sommer/raku/check-color
new file mode 100755
index 0000000000..e1da604601
--- /dev/null
+++ b/challenge-281/arne-sommer/raku/check-color
@@ -0,0 +1,13 @@
+#! /usr/bin/env raku
+
+subset BOARDPOS where * ~~ /^<[a..h]><[1..8]>$/;
+
+unit sub MAIN (BOARDPOS $coordinate, :v(:$verbose));
+
+my ($col, $row) = $coordinate.comb;
+
+say ": Row $row, Col $col" if $verbose;
+
+say $col eq any(<a c e g>)
+ ?? $row %% 2
+ !! $row !%% 2;
diff --git a/challenge-281/arne-sommer/raku/check-color-boring b/challenge-281/arne-sommer/raku/check-color-boring
new file mode 100755
index 0000000000..297604368b
--- /dev/null
+++ b/challenge-281/arne-sommer/raku/check-color-boring
@@ -0,0 +1,19 @@
+#! /usr/bin/env raku
+
+subset BOARDPOS where * ~~ /^<[a..h]><[1..8]>$/;
+
+unit sub MAIN (BOARDPOS $coordinate);
+
+my %l;
+
+%l<a2> = %l<a4> = %l<a6> = %l<a8> = True;
+%l<b1> = %l<b3> = %l<b5> = %l<b7> = True;
+%l<c2> = %l<c4> = %l<c6> = %l<c8> = True;
+%l<d1> = %l<d3> = %l<d5> = %l<d7> = True;
+
+%l<e2> = %l<e4> = %l<e6> = %l<e8> = True;
+%l<f1> = %l<f3> = %l<f5> = %l<f7> = True;
+%l<g2> = %l<g4> = %l<g6> = %l<g8> = True;
+%l<h1> = %l<h3> = %l<h5> = %l<h7> = True;
+
+say so %l{$coordinate};
diff --git a/challenge-281/arne-sommer/raku/knightmover b/challenge-281/arne-sommer/raku/knightmover
new file mode 100755
index 0000000000..377739fecb
--- /dev/null
+++ b/challenge-281/arne-sommer/raku/knightmover
@@ -0,0 +1,44 @@
+#! /usr/bin/env raku
+
+subset BOARDPOS where * ~~ /^<[a..h]><[1..8]>$/;
+subset MOVE of Int where -7 <= * <= 7;
+
+unit sub MAIN (BOARDPOS $start, BOARDPOS $end, :v(:$verbose));
+
+my %seen = ( $start => True );
+
+my @queue = \{ steps => 0, pos => $start, path => ($start,) };
+
+while @queue
+{
+ my %curr = @queue.shift;
+
+ say ": Steps %curr<steps>: at pos %curr<pos> (path: %curr<path>)" if $verbose;
+
+ if (%curr<pos> eq $end)
+ {
+ say ": Arrived at target after %curr<steps> steps (path: %curr<path>)" if $verbose;
+ say %curr<steps>;
+ last;
+ }
+
+ for ( (-2,-1), (-2,1), (2,-1), (2,1), (-1,-2), (-1,2), (1,-2), (1,2)) -> ($row-offset, $col-offset)
+ {
+ my $new-pos = new-pos-knight(%curr<pos>, c => $col-offset, r => $row-offset) // next;
+ next if %seen{$new-pos}++;
+ @queue.push: { pos => $new-pos, steps => %curr<steps> +1, path => ( %curr<path>, $new-pos).flat };
+ }
+}
+
+sub new-pos-knight (BOARDPOS $pos, MOVE :r(:$row-offset), MOVE :c(:$col-offset))
+{
+ my ($col, $row) = $pos.comb;
+
+ my $new-row = $row + $row-offset;
+ return unless 1 <= $new-row <= 8;
+
+ my $new-col = (ord($col) + $col-offset).chr;
+ return unless $new-col eq any(<a b c d e f g h>);
+
+ return $new-col ~ $new-row;
+} \ No newline at end of file