aboutsummaryrefslogtreecommitdiff
path: root/challenge-118
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-06-26 22:33:01 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-06-26 22:33:01 +0100
commit0d0d5481ca9a3f18403eb433a8b00df549892773 (patch)
tree25c0a8c5e84ce26212c5995d49f0f4fefe4aa145 /challenge-118
parent591dae4c8b7fff697c7baafd3531729e68faa79b (diff)
downloadperlweeklychallenge-club-0d0d5481ca9a3f18403eb433a8b00df549892773.tar.gz
perlweeklychallenge-club-0d0d5481ca9a3f18403eb433a8b00df549892773.tar.bz2
perlweeklychallenge-club-0d0d5481ca9a3f18403eb433a8b00df549892773.zip
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-118')
-rw-r--r--challenge-118/arne-sommer/blog.txt1
-rw-r--r--challenge-118/arne-sommer/perl/ch-1.pl20
-rw-r--r--challenge-118/arne-sommer/raku/ch-1.raku9
-rw-r--r--challenge-118/arne-sommer/raku/ch-2.raku62
4 files changed, 92 insertions, 0 deletions
diff --git a/challenge-118/arne-sommer/blog.txt b/challenge-118/arne-sommer/blog.txt
new file mode 100644
index 0000000000..f3efbb4875
--- /dev/null
+++ b/challenge-118/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/binary-knight.html
diff --git a/challenge-118/arne-sommer/perl/ch-1.pl b/challenge-118/arne-sommer/perl/ch-1.pl
new file mode 100644
index 0000000000..1f64526fab
--- /dev/null
+++ b/challenge-118/arne-sommer/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#! /usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use Getopt::Long;
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my $N = shift(@ARGV);
+
+die "Specify a positive integer" unless $N =~ /^[1-9]\d*$/;
+
+my $bin = sprintf('%b', $N);
+
+say ": $bin (binary)\n: " . reverse($bin) . " (binary flipped)" if $verbose;
+
+say 0 + ($bin eq reverse($bin)); \ No newline at end of file
diff --git a/challenge-118/arne-sommer/raku/ch-1.raku b/challenge-118/arne-sommer/raku/ch-1.raku
new file mode 100644
index 0000000000..1daa749dca
--- /dev/null
+++ b/challenge-118/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,9 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $N where $N > 0, :v($verbose));
+
+my $bin = $N.fmt('%b');
+
+say ": $bin (binary)\n: { $bin.flip } (binary flipped)" if $verbose;
+
+say + ($bin eq $bin.flip); \ No newline at end of file
diff --git a/challenge-118/arne-sommer/raku/ch-2.raku b/challenge-118/arne-sommer/raku/ch-2.raku
new file mode 100644
index 0000000000..9d8b8fac0d
--- /dev/null
+++ b/challenge-118/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,62 @@
+#! /usr/bin/env raku
+
+subset ChessPos of Str where
+ $_.chars == 2 &&
+ $_.substr(0,1) ~~ /<[abcdefgh]>/ &&
+ $_.substr(1,1) ~~ /<[12345678]>/;
+
+unit sub MAIN (ChessPos $pos = 'a8', :v($verbose));
+
+my ($col, $row) = $pos.comb;
+
+my %treasures = ('e6' => True, 'c4' => True, 'b3' => True,
+ 'a2' => True, 'b2' => True, 'b1' => True);
+
+my %visited = ('a8' => True);
+
+my @todo = ( ('a8', 'a8', {%visited}, {%treasures}), );
+
+while @todo
+{
+ my $next = @todo.shift;
+ my ($pos, $path, $visited, $treasures) = @($next);
+ my %visited = %($visited);
+ my %treasures = %($treasures);
+
+ for get-next($pos) -> $next
+ {
+ say ": Checking pos: $next (at path \'$path\') with { %visited.elems } visits and { %treasures.elems } remaining treasures" if $verbose;
+ next if %visited{$next};
+
+ if %treasures{$next}
+ {
+ if %treasures.elems == 1
+ {
+ say "Path: $path $next";
+ exit;
+ }
+
+ %treasures{$next}:delete;
+ }
+ %visited{$next} = True;
+ @todo.push: ($next, "$path $next", {%visited}, {%treasures});
+ }
+}
+
+sub get-next ($pos)
+{
+ my ($col, $row) = $pos.comb;
+
+ my @next = (
+ "{ ($col.ord - 2).chr }{ $row - 1 }", # LLD
+ "{ ($col.ord - 2).chr }{ $row + 1 }", # LLU
+ "{ ($col.ord - 1).chr }{ $row - 2 }", # LDD
+ "{ ($col.ord - 1).chr }{ $row + 2 }", # LUU
+ "{ ($col.ord + 1).chr }{ $row - 2 }", # RDD
+ "{ ($col.ord + 1).chr }{ $row + 2 }", # RUU
+ "{ ($col.ord + 2).chr }{ $row - 1 }", # RRD
+ "{ ($col.ord + 2).chr }{ $row + 1 }", # RRU
+ ).grep( * ~~ ChessPos);
+
+ return @next;
+} \ No newline at end of file