diff options
| author | rir <rirans@comcast.net> | 2024-08-08 10:55:43 -0400 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-08-08 10:57:04 -0400 |
| commit | 039db27e578c2d6795ff912ad9db80b99d9f93b8 (patch) | |
| tree | acfe3da8f5b65f1059be662442368b7db5900446 | |
| parent | 50188dcd5738b428fd3e9ca75839fc5f8e2df12c (diff) | |
| download | perlweeklychallenge-club-039db27e578c2d6795ff912ad9db80b99d9f93b8.tar.gz perlweeklychallenge-club-039db27e578c2d6795ff912ad9db80b99d9f93b8.tar.bz2 perlweeklychallenge-club-039db27e578c2d6795ff912ad9db80b99d9f93b8.zip | |
281
| -rw-r--r-- | challenge-281/0rir/raku/Chess.rakumod | 15 | ||||
| -rw-r--r-- | challenge-281/0rir/raku/ch-1.raku | 84 | ||||
| -rw-r--r-- | challenge-281/0rir/raku/ch-2.raku | 106 |
3 files changed, 205 insertions, 0 deletions
diff --git a/challenge-281/0rir/raku/Chess.rakumod b/challenge-281/0rir/raku/Chess.rakumod new file mode 100644 index 0000000000..85809b7d62 --- /dev/null +++ b/challenge-281/0rir/raku/Chess.rakumod @@ -0,0 +1,15 @@ + +unit class Chess; + +=begin comment + Chess -- Miscellany supporting definitions of-- and actions on-- a + chess board. +=end comment + +constant @square is export = [ 0…63]; # a chessboard + +our @file-name is export = [ 'a' … 'h']; +our @rank-name is export = [ 1 … 8]; +our @algebraic is export = ( @file-name X[~] @rank-name).sort( {.flip}); +our %algebraic2sq is export = @algebraic Z[=>] @square; +sub algebraic2sq( Str $a --> Int) is export { %algebraic2sq{$a} } diff --git a/challenge-281/0rir/raku/ch-1.raku b/challenge-281/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..23b9cec767 --- /dev/null +++ b/challenge-281/0rir/raku/ch-1.raku @@ -0,0 +1,84 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent; +use Test; + +use Chess; + +=begin comment +281-1: Check Color Submitted by: Mohammad Sajid Anwar +Restated +You are given coordinates, a string that represents the coordinates of a +square of the chessboard using the standard algebraic naming, Looking at +the board from the white player's side and with a dark square nearest +at white's left, the files (columns ) are named, from left to right, +'a' to 'h', and the ranks (rows) are numbered, from nearest to farthest, +'1' to '8'. So the white player's left hand corner is 'a1' and blacks's +is 'h8'. + +Return true if the square is light, and false if the square is dark. + +Example 1 +Input: $coordinates = "d3" +Output: true +Example 2 +Input: $coordinates = "g5" +Output: false +Example 3 +Input: $coordinates = "e6" +Output: true + +=end comment + +my @Test = + # @input $exp + [< a1 a3 a5 a7 b2 b4 b6 b8 + c1 c3 c5 c7 d2 d4 d6 d8 + e1 e3 e5 e7 f2 f4 f6 f8 + g1 g3 g5 g7 h2 h4 h6 h8 >], False, + + [< a2 a4 a6 a8 b1 b3 b5 b7 + c2 c4 c6 c8 d1 d3 d5 d7 + e2 e4 e6 e8 f1 f3 f5 f7 + g2 g4 g6 g8 h1 h3 h5 h7 >], True, +; +plan 192; + +sub task( $a) is pure { + so ( %algebraic2sq{$a} div 8 + %algebraic2sq{$a} % 8 ) % 2 +} + +my %light-alge = + < a2 a4 a6 a8 b1 b3 b5 b7 c2 c4 c6 c8 d1 d3 d5 d7 + e2 e4 e6 e8 f1 f3 f5 f7 g2 g4 g6 g8 h1 h3 h5 h7 > + Z[=>] True xx 32; + +sub task1( Str $a) is pure { %light-alge{$a} or False } + +sub not-task( Int $sq) is pure { + so ( $sq div 8 + $sq % 8 ) % 2 +} +for @Test -> @in, $exp { + for @in -> $in { + is task($in), $exp, "$exp <- $in"; + is task1($in), $exp, "$exp <- $in"; + is not-task(algebraic2sq($in)), $exp, "$exp <- $in"; + } +} + +done-testing; + +my $coordinates = 'g7'; +say qq{\nInput: \$coordinates = "$coordinates"\nOutput: &task1($coordinates)}; + + +=finish + +use Bench; my $b = Bench.new; +my $sq = 63, +$b.timethese( 64000, { + alge-mod-mod => { $coordinates.&task }, + mod-mod => { $sq.¬-task }, + alge-lookup => { $coordinates.&task1 }, +}); diff --git a/challenge-281/0rir/raku/ch-2.raku b/challenge-281/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..53904cbce4 --- /dev/null +++ b/challenge-281/0rir/raku/ch-2.raku @@ -0,0 +1,106 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent; +use Test; + +use Chess; + +=begin comment + +281-2: Knight’s Move Submitted by: Peter Campbell Smith +Restated +Write a script which takes a knight's starting position and its ending +position, in algebraic notation, and calculates the least number of +moves required. + +Example 1 +Input: $start = 'g2', $end = 'a8' +Ouput: 4 + +g2 -> e3 -> d5 -> c7 -> a8 +Example 2 +Input: $start = 'g2', $end = 'h2' +Ouput: 3 + +g2 -> e3 -> f1 -> h2 + +=end comment + +my @Test = + # point point moves + 'g2', 'a1', 3, + 'g2', 'h1', 4, + 'g2', 'a8', 4, + 'g2', 'h8', 3, + 'g2', 'h2', 3, + + 'a1', 'a2', 3, 'a1', 'a3', 2, 'a1', 'a4', 3, 'a1', 'a5', 2, + 'a1', 'a6', 3, 'a1', 'a7', 4, 'a1', 'a8', 5, + 'a1', 'b1', 3, 'a1', 'b2', 4, 'a1', 'b3', 1, 'a1', 'b4', 2, + 'a1', 'b5', 3, 'a1', 'b6', 4, 'a1', 'b7', 3, 'a1', 'b8', 4, + 'a1', 'c1', 2, 'a1', 'c2', 1, 'a1', 'c3', 4, 'a1', 'c4', 3, + 'a1', 'c5', 2, 'a1', 'c6', 3, 'a1', 'c7', 4, 'a1', 'c8', 5, + 'a1', 'd1', 3, 'a1', 'd2', 2, 'a1', 'd3', 3, 'a1', 'd4', 2, + 'a1', 'd5', 3, 'a1', 'd6', 4, 'a1', 'd7', 3, 'a1', 'd8', 4, + 'a1', 'e1', 2, 'a1', 'e2', 3, 'a1', 'e3', 2, 'a1', 'e4', 3, + 'a1', 'e5', 4, 'a1', 'e6', 3, 'a1', 'e7', 4, 'a1', 'e8', 5, + 'a1', 'f1', 3, 'a1', 'f2', 4, 'a1', 'f3', 3, 'a1', 'f4', 4, + 'a1', 'f5', 3, 'a1', 'f6', 4, 'a1', 'f7', 5, 'a1', 'f8', 4, + 'a1', 'g1', 4, 'a1', 'g2', 3, 'a1', 'g3', 4, 'a1', 'g4', 3, + 'a1', 'g5', 4, 'a1', 'g6', 5, 'a1', 'g7', 4, 'a1', 'g8', 5, + 'a1', 'h1', 5, 'a1', 'h2', 4, 'a1', 'h3', 5, 'a1', 'h4', 4, + 'a1', 'h5', 5, 'a1', 'h6', 4, 'a1', 'h7', 5, 'a1', 'h8', 6, +; +plan 2 × @Test ÷ 3; + +my @corner = [ 0,7,56,63]; + +# Find the dog leg twixt two squares. +sub Δ( Int $a, Int $b --> Array) is pure { + ( ($a div 8 - $b div 8).abs, # rank delta + ($a % 8 - $b % 8).abs, # file delta + ).sort.Array; +} + +my @raw-dog-leg-data = [0, 1, 3], [0, 2, 2], [0, 3, 3], [0, 4, 2], + [0, 5, 3], [0, 6, 4], [0, 7, 5], + + [1, 1, 2], # corner case 4 + [1, 2, 1], [1, 3, 2], [1, 4, 3], [1, 5, 4], + [1, 6, 3], [1, 7, 4], + + [2, 2, 4], [2, 3, 3], [2, 4, 2], [2, 5, 3], [2, 6, 4], [2, 7, 5], + [3, 3, 2], [3, 4, 3], [3, 5, 4], [3, 6, 3], [3, 7, 4], + [4, 4, 4], [4, 5, 3], [4, 6, 4], [4, 7, 5], + [5, 5, 4], [5, 6, 5], [5, 7, 4], + [6, 6, 4], [6, 7, 5], + [7, 7, 6]; + +my %Δ2N-ct{Int}; # Map from dog leg to N move ct. + +for @raw-dog-leg-data -> @a { %Δ2N-ct{ @a[0] }{@a[1]} = @a[2] } + +# Knight move count to traverse a dogleg on the board. +sub N-sq2sq-ct( Int $a, Int $b -->Int ) is pure { + my @leg = Δ( $a, $b); + die 'There is a compulsion to move' if @leg ~~ [0, 0]; #explicate intent? + return 4 if @leg ~~ [1,1] and so [$a, $b].any == @corner.any; + return %Δ2N-ct{ @leg[0]}{@leg[1] } +} + +# Return move count for Knight to get from one named sq to another. +sub task( $a, $b -->Int) is pure { + N-sq2sq-ct( $a.&algebraic2sq, $b.&algebraic2sq); +} + +for @Test -> $a, $b, $exp { + is task($a, $b), $exp, "$exp <- $a ∘∘ $b"; + is task($b, $a), $exp, "$exp <- $b ∘∘ $a"; +} +done-testing; + +my ($start, $end ) = 'g2', 'h1'; + +say "\nInput: \$start = $start, \$end = $end" + ~ "\nOutput: &task($end,$start)"; |
