aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-06 09:52:25 +0100
committerGitHub <noreply@github.com>2024-08-06 09:52:25 +0100
commitbd052149fc1ed2e35988285aac9975cac002a039 (patch)
tree6d549ebc0819814ae205efcca3b99d6bd918448b
parent76658583af9562a94eaf9bee1ade1b8f6d039798 (diff)
parent5a74f63a80819bcaa7048779dadd508e09b114e5 (diff)
downloadperlweeklychallenge-club-bd052149fc1ed2e35988285aac9975cac002a039.tar.gz
perlweeklychallenge-club-bd052149fc1ed2e35988285aac9975cac002a039.tar.bz2
perlweeklychallenge-club-bd052149fc1ed2e35988285aac9975cac002a039.zip
Merge pull request #10550 from wlmb/challenges
Solve PWC281
-rw-r--r--challenge-281/wlmb/blog.txt1
-rwxr-xr-xchallenge-281/wlmb/perl/ch-1.pl19
-rwxr-xr-xchallenge-281/wlmb/perl/ch-2.pl42
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-281/wlmb/blog.txt b/challenge-281/wlmb/blog.txt
new file mode 100644
index 0000000000..f505524efa
--- /dev/null
+++ b/challenge-281/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2024/08/04/PWC281/
diff --git a/challenge-281/wlmb/perl/ch-1.pl b/challenge-281/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..94438ac772
--- /dev/null
+++ b/challenge-281/wlmb/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 281
+# Task 1: Check Color
+#
+# See https://wlmb.github.io/2024/08/04/PWC281/#task-1-check-color
+use v5.36;
+die <<~"FIN" unless @ARGV;
+ Usage $0 P1 P2...
+ to test whether position Pi in a chessboard corresponds to a white square,
+ where Pi is of the form xy with x a letter in the range a-h and y a digit
+ in the range 1-8.
+ FIN
+for(@ARGV){
+ warn("Bad format: $_"), next unless /^([a-hA-H])([1-7])$/;
+ my $x=ord(lc $1)-ord("a"); # convert to number 0-7
+ my $y=$2-1;
+ my $result=($x+$y)%2?"True":"False";
+ say "$_ -> $result";
+}
diff --git a/challenge-281/wlmb/perl/ch-2.pl b/challenge-281/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..9911a6cb36
--- /dev/null
+++ b/challenge-281/wlmb/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 281
+# Task 2: Knight’s Move
+#
+# See https://wlmb.github.io/2024/08/04/PWC281/#task-2-knight’s-move
+use v5.36;
+use PDL;
+use PDL::NiceSlice;
+use experimental qw(for_list);
+die <<~"FIN" unless @ARGV && @ARGV%2==0;
+ Usage: $0 S1 E1...
+ to find how many moves are required for a knight to go from position Si
+ to position Ei in a chessboard.
+ The positions are given in the format xy where x is a letter in the
+ range a-h and denotes the horizontal position while y is a digit between 1 and 8
+ and denotes vertical position.
+ FIN
+my $moves=pdl[[1,2],[2,1]]; # Construct all moves of knight
+my $signs=pdl[1,-1];
+my $allmoves=pdl($moves, $moves*$signs, -$moves*$signs,-$moves)->clump(1,2);
+for my($start, $end)(@ARGV){
+ my ($current, $goal)=map{ # convert to coordinate vectors, origin at (0,0)
+ warn("Bad format: $_"), next unless /^([a-hA-H])([1-8])$/;
+ pdl(ord(lc $1)-ord("a"), $2-1)
+ } ($start, $end);
+ my $iteration=0;
+ my $visited=zeroes(8,8);
+ $visited->indexND($current).=1;
+ until(($current==$goal)->andover->any){ # until we reach the goal
+ my $next=($current+$allmoves->dummy(1))->clump(1,2); # jump to new positions
+ $current=$next
+ ->dice_axis( # remove invalid positions
+ 1, which(($next((0))>=0)&($next((0))<8)&($next((1))>=0)&($next((1))<8)))
+ ->uniqvec; # remove duplicates
+ $current=$current->dice_axis( # remove previously visited
+ 1, which(!$visited->indexND($current))
+ );
+ $visited->indexND($current).=1;
+ ++$iteration;
+ }
+ say "From $start to $end -> $iteration movements";
+}