aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2024-08-08 23:32:30 -0400
committerJaldhar H. Vyas <jaldhar@braincells.com>2024-08-11 12:46:25 -0400
commit374396ea25ddc0fb1da2a2b16b9057a88dfcc40d (patch)
treeacb49f9b4cf82e45aec5a32cbab230fbf5bfa051
parent10621ff55d86161c732fef4c098784d7b35fcb4e (diff)
downloadperlweeklychallenge-club-374396ea25ddc0fb1da2a2b16b9057a88dfcc40d.tar.gz
perlweeklychallenge-club-374396ea25ddc0fb1da2a2b16b9057a88dfcc40d.tar.bz2
perlweeklychallenge-club-374396ea25ddc0fb1da2a2b16b9057a88dfcc40d.zip
Challenge 281 by Jaldhar H. Vyas.
-rw-r--r--challenge-281/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-281/jaldhar-h-vyas/perl/ch-1.sh3
-rwxr-xr-xchallenge-281/jaldhar-h-vyas/perl/ch-2.pl140
-rwxr-xr-xchallenge-281/jaldhar-h-vyas/raku/ch-1.sh3
-rwxr-xr-xchallenge-281/jaldhar-h-vyas/raku/ch-2.raku133
5 files changed, 280 insertions, 0 deletions
diff --git a/challenge-281/jaldhar-h-vyas/blog.txt b/challenge-281/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..de86752708
--- /dev/null
+++ b/challenge-281/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2024/08/perl_weekly_challenge_week_281.html
diff --git a/challenge-281/jaldhar-h-vyas/perl/ch-1.sh b/challenge-281/jaldhar-h-vyas/perl/ch-1.sh
new file mode 100755
index 0000000000..241503a868
--- /dev/null
+++ b/challenge-281/jaldhar-h-vyas/perl/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+perl -E '$s;map{$s += ord}split//,shift; say $s%2!=1?"false":"true"' "$@"
diff --git a/challenge-281/jaldhar-h-vyas/perl/ch-2.pl b/challenge-281/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..29a58eb2c0
--- /dev/null
+++ b/challenge-281/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,140 @@
+#!/usr/bin/perl
+use v5.38;
+
+package Position;
+use Moo;
+use namespace::clean;
+use overload '==' => \&compare;
+
+has row => (
+ is => 'rw',
+);
+
+has col => (
+ is => 'rw',
+);
+
+around BUILDARGS => sub($orig, $class, @args) {
+ return { row => $args[0], col => $args[1] };
+};
+
+sub str {
+ my ($self) = @_;
+ return chr(ord('a') + $self->col) . (8 - $self->row);
+}
+
+sub compare {
+ my ($self, $other) = @_;
+ return $self->row == $other->row && $self->col == $other->col;
+}
+
+1;
+
+package main;
+
+sub estimatedCost {
+ return 1;
+}
+
+sub stepCost($position) {
+ return 1;
+}
+
+sub tryMove($position, $delta) {
+
+ my $dest = Position->new($position->row + $delta->row, $position->col + $delta->col);
+ return ($dest->row >= 0 && $dest->row < 8 && $dest->col >= 0 && $dest->col < 8)
+ ? $dest
+ : undef;
+}
+
+sub possibleMoves($position, $target) {
+
+ state @deltas = (
+ Position->new(-2, -1),
+ Position->new(-2, 1),
+ Position->new(-1, 2),
+ Position->new(1, 2),
+ Position->new(2, 1),
+ Position->new(2, -1),
+ Position->new(-1, -2),
+ Position->new(1, -2)
+ );
+
+ my @moves;
+ for my $delta (@deltas) {
+ my $move = tryMove($position, $delta);
+ if (defined $move) {
+ push @moves, $move;
+ }
+ }
+
+ @moves = sort { estimatedCost($a, $target) < estimatedCost($b, $target); } @moves;
+
+ return @moves;
+}
+
+sub search($path, $target, $cost, $bound) {
+ my $current = $path->[-1];
+
+ my $estimate = $cost + estimatedCost($current, $target);
+ if ($estimate > $bound) {
+ return $estimate;
+ }
+
+ if ($current == $target) {
+ return '-inf';
+ }
+
+ my $min = 'inf';
+
+ for my $move (possibleMoves($current, $target)) {
+ if (!grep { $_ == $move } @{$path}) {
+ push @{$path}, $move;
+ my $t = search($path, $target, $cost + stepCost($move), $bound);
+
+ if ($t == '-inf') {
+ return '-inf';
+ }
+
+ if ($t < $min) {
+ $min = $t;
+ }
+
+ pop @{$path};
+ }
+ }
+
+ return $min;
+}
+
+sub makePath($current, $target) {
+ my @path = ( $current );
+
+ my $bound = estimatedCost($current, $target);
+
+ while(1) {
+ my $t = search(\@path, $target, 0, $bound);
+
+ if ($t == '-inf') {
+ last;
+ }
+
+ # Can't solve; this shouldn't happen.
+ if ($t == 'inf') {
+ last;
+ }
+
+ $bound = $t;
+ }
+
+ return @path;
+}
+
+my ($sc, $sr) = split //, shift;
+my ($ec, $er) = split //, shift;
+
+my $start = Position->new(ord('8') - ord($sr), ord($sc) - ord('a'));
+my $end = Position->new(ord('8') - ord($er), ord($ec) - ord('a'));
+
+say scalar makePath($start, $end) - 1; \ No newline at end of file
diff --git a/challenge-281/jaldhar-h-vyas/raku/ch-1.sh b/challenge-281/jaldhar-h-vyas/raku/ch-1.sh
new file mode 100755
index 0000000000..017e624b15
--- /dev/null
+++ b/challenge-281/jaldhar-h-vyas/raku/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+raku -e 'say @*ARGS[0].comb.map({.ord}).sum!%%2' "$@"
diff --git a/challenge-281/jaldhar-h-vyas/raku/ch-2.raku b/challenge-281/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..051342a521
--- /dev/null
+++ b/challenge-281/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,133 @@
+#!/usr/bin/raku
+
+class Position {
+ has Int $.row is rw;
+ has Int $.col is rw;
+
+ method new( $row, $col ) {
+ self.bless(:$row, :$col);
+ }
+
+ method Str {
+ ('a'.ord + $!col).chr ~ (8 - $!row).Str;
+ }
+}
+
+multi sub infix:<==>(Position $a, Position $b) returns Bool {
+ return $a.row == $b.row && $a.col == $b.col;
+}
+
+sub estimatedCost(Position $position, Position $goal) {
+ return 1;
+}
+
+sub stepCost(Position $position) {
+ return 1;
+}
+
+
+sub tryMove(Position $position, Position $delta) {
+ my $dest = $position.clone;
+ $dest.row += $delta.row;
+ $dest.col += $delta.col;
+ return ($dest.row >= 0 && $dest.row < 8 && $dest.col >= 0 && $dest.col < 8)
+ ?? $dest
+ !! Nil;
+}
+
+sub possibleMoves(Position $position, Position $target) {
+ state @deltas = [
+ Position.new(-2, -1),
+ Position.new(-2, 1),
+ Position.new(-1, 2),
+ Position.new(1, 2),
+ Position.new(2, 1),
+ Position.new(2, -1),
+ Position.new(-1, -2),
+ Position.new(1, -2)
+ ];
+
+ my @moves;
+ for @deltas -> $delta {
+ my $move = tryMove($position, $delta);
+ if $move {
+ @moves.push($move);
+ }
+ }
+
+ @moves = @moves.sort({
+ estimatedCost($^a, $target) < estimatedCost($^b, $target);
+ });
+
+ return @moves;
+}
+
+sub search(Position @path, Position $target, Int $cost, Int $bound) {
+ my $current = @path[*-1];
+
+ my $estimate = $cost + estimatedCost($current, $target);
+ if $estimate > $bound {
+ return $estimate;
+ }
+
+ if $current == $target {
+ return -∞;
+ }
+
+ my $min = ∞;
+
+ for possibleMoves($current, $target) -> $move {
+ if $move ⊄ @path {
+ @path.push($move);
+ my $t = search(@path, $target, $cost + stepCost($move), $bound);
+
+ if $t == -∞ {
+ return -∞;
+ }
+
+ if $t < $min {
+ $min = $t;
+ }
+
+ @path.pop;
+ }
+ }
+
+ return $min;
+}
+
+sub makePath(Position $current, Position $target) {
+ my Position @path = [ $current ];
+ my $bound = estimatedCost($current, $target);
+
+ loop {
+ my $t = search(@path, $target, 0, $bound);
+
+ if $t ~~ -∞ {
+ last;
+ }
+
+ # Can't solve; this shouldn't happen.
+ if $t ~~ ∞ {
+ last;
+ }
+
+ $bound = $t;
+ }
+
+ return @path;
+}
+
+sub MAIN(
+ Str $s where $s.chars == 2 ,
+ Str $e where $e.chars == 2
+) {
+
+ my ($sc, $sr) = $s.comb;
+ my ($ec, $er) = $e.comb;
+ my $start = Position.new('8'.ord - $sr.ord, $sc.ord - 'a'.ord );
+ my $end = Position.new('8'.ord - $er.ord, $ec.ord - 'a'.ord);
+
+ say makePath($start, $end).elems - 1;
+
+} \ No newline at end of file