diff options
| -rw-r--r-- | challenge-281/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-281/arne-sommer/raku/ch-1.raku | 13 | ||||
| -rwxr-xr-x | challenge-281/arne-sommer/raku/ch-2.raku | 44 | ||||
| -rwxr-xr-x | challenge-281/arne-sommer/raku/check-color | 13 | ||||
| -rwxr-xr-x | challenge-281/arne-sommer/raku/check-color-boring | 19 | ||||
| -rwxr-xr-x | challenge-281/arne-sommer/raku/knightmover | 44 |
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 |
