aboutsummaryrefslogtreecommitdiff
path: root/challenge-281
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-08-08 16:36:22 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-08-08 16:36:22 +0100
commit47c4cea4080b4051d024685bedd5290e205cca74 (patch)
tree285784b285e8dd84e589cc249d5a1eb06ac14a4f /challenge-281
parent4a8ca815a012b6121ae212509865ff9c40e87d7d (diff)
downloadperlweeklychallenge-club-47c4cea4080b4051d024685bedd5290e205cca74.tar.gz
perlweeklychallenge-club-47c4cea4080b4051d024685bedd5290e205cca74.tar.bz2
perlweeklychallenge-club-47c4cea4080b4051d024685bedd5290e205cca74.zip
- Added solutions by Luca Ferrari.
- Added solutions by Robert Ransbottom. - Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-281')
-rw-r--r--challenge-281/laurent-rosenfeld/blog1.txt1
-rw-r--r--challenge-281/laurent-rosenfeld/perl/ch-2.pl45
-rw-r--r--challenge-281/laurent-rosenfeld/raku/ch-2.raku40
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-281/laurent-rosenfeld/blog1.txt b/challenge-281/laurent-rosenfeld/blog1.txt
new file mode 100644
index 0000000000..a825148645
--- /dev/null
+++ b/challenge-281/laurent-rosenfeld/blog1.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/laurent_r/2024/08/perl-weekly-challenge-281-knights-move.html
diff --git a/challenge-281/laurent-rosenfeld/perl/ch-2.pl b/challenge-281/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..78b804f8bd
--- /dev/null
+++ b/challenge-281/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,45 @@
+use strict;
+use warnings;
+use feature 'say';
+
+my @moves = ( [<2 1>], [<2 -1>], [<1 2>], [<1 -2>],
+ [<-1 2>], [<-1 -2>], [<-2 1>], [<-2 -1>] );
+
+sub to_coordinates {
+ my ($col, $row) = split //, shift;
+ return ord($col) - ord('a'), $row - 1;
+}
+
+sub find_shortest {
+ my ($st_in, $end_in) = @_;
+ # convert input to Cartesian coordinates
+ my @start = to_coordinates $st_in;
+ my @end = to_coordinates $end_in;
+
+ my @to_be_explored; # a queue of squares to be visited
+ push @to_be_explored, [0, @start];
+ my %seen = ("@start" => 1); # already visited squares
+ while (@to_be_explored) {
+ my $node = shift @to_be_explored;
+ my ($depth, @current) = @$node;
+ return $depth if "@current" eq "@end";
+ for my $move (@moves) {
+ my @next = ( $current[0] + $move->[0],
+ $current[1] + $move->[1] );
+ # dismiss if computed position not on chessboard
+ next if $next[0] > 7 or $next[0] < 0 or
+ $next[1] > 7 or $next[1] < 0;
+ # dismiss if computed position already visited
+ next if exists $seen{"@next"};
+ # update seen hash and to_be_explored queue
+ $seen{"@next"} = 1;
+ push @to_be_explored, [$depth + 1, @next];
+ }
+ }
+}
+
+my @tests = ([<g2 a8>], [<g2 h2>]);
+for my $test (@tests) {
+ printf "%-6s => ", "@$test";
+ say find_shortest @$test;;
+}
diff --git a/challenge-281/laurent-rosenfeld/raku/ch-2.raku b/challenge-281/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..930a2ec7fe
--- /dev/null
+++ b/challenge-281/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,40 @@
+my @moves = <2 1>, <2 -1>, <1 2>, <1 -2>,
+ <-1 2>, <-1 -2>, <-2 1>, <-2 -1>;
+
+sub to-coordinates ($in) {
+ my ($col, $row) = $in.comb;
+ return $col.ord - 'a'.ord, $row - 1;
+}
+
+sub find-shortest ($st-in, $end-in) {
+ # convert input to Cartesian coordinates
+ my @start = to-coordinates $st-in;
+ my @end = to-coordinates $end-in;
+
+ my @to-be-explored; # a queue of squares to be visited
+ push @to-be-explored, (0, @start).flat;
+ my %seen = "@start[]" => 1; # already visited squares
+
+ while @to-be-explored {
+ my @node = shift @to-be-explored;
+ my ($depth, @current) = @node[0];
+ return $depth if "@current[]" eq "@end[]";
+ for @moves -> @move {
+ my @next = @current[0] + @move[0],
+ @current[1] + @move[1];
+ # dismiss if computed position not on chessboard
+ next if @next.any > 7 or @next.any < 0;
+ # dismiss if computed position already visited
+ next if %seen{"@next[]"}:exists;
+ # update seen hash and to-be-explored queue
+ %seen{"@next[]"} = 1;
+ push @to-be-explored, ($depth + 1, @next).flat;
+ }
+ }
+}
+
+my @tests = <g2 a8>, <g2 h2>;
+for @tests -> @test {
+ printf "%-6s => ", "@test[]";
+ say find-shortest @test[0], @test[1];
+}