diff options
| -rw-r--r-- | challenge-281/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-281/dave-jacoby/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-281/dave-jacoby/perl/ch-2.pl | 99 |
3 files changed, 122 insertions, 0 deletions
diff --git a/challenge-281/dave-jacoby/blog.txt b/challenge-281/dave-jacoby/blog.txt new file mode 100644 index 0000000000..fd60fc879a --- /dev/null +++ b/challenge-281/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2024/08/05/pawning-things-off-weekly-challenge-281.html diff --git a/challenge-281/dave-jacoby/perl/ch-1.pl b/challenge-281/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..ca81d9bcd6 --- /dev/null +++ b/challenge-281/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +my @examples = (qw{ d3 g5 e6 }); + +for my $example (@examples) { + my $output = color_check($example); + say <<"END"; + Input: \@str = "$example" + Output: $output +END +} + +sub color_check ($input) { + my ( $l, $n ) = split //, $input; + my @lets = 'a' .. 'h'; + my %lets = map { $lets[$_] => $_ } 0 .. -1 + scalar @lets; + return ( $lets{$l} + ( $n % 2 ) ) % 2 ? 'false' : 'true '; +} diff --git a/challenge-281/dave-jacoby/perl/ch-2.pl b/challenge-281/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..84a7402b54 --- /dev/null +++ b/challenge-281/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( # added a couple test entries + + [ 'g2', 'a8' ], + [ 'g2', 'h2' ], + [ 'a1', 'h8' ], + [ 'd5', 'e4' ], +); + +for my $input (@examples) { + my $output = knights_move($input); + my ( $start, $end ) = $input->@*; + say <<"END"; + Input: \$start = "$start", \$end = "$end" + Output: $output +END +} + +sub knights_move($input) { + my ( $start, $end ) = $input->@*; + my $board = {}; # used for display + my $done = {}; # used to track the win + my @end = split //, $end; + $board->{ $end[0] }{ $end[1] } = -2; + $done->{$end} = 'E'; + + + # these get us from 'a1' to [7,0] and back + my @lets = 'a' .. 'h'; + my %lets = map { $lets[$_] => $_ } 0 .. -1 + scalar @lets; + my %stel = reverse %lets; + + my @nums = reverse 1 .. 8; + my %nums = map { $nums[$_] => $_ } 0 .. -1 + scalar @nums; + my %smun = reverse %nums; + + my @moves = ( [ $start, 0 ] ); + my @jumps = ( + [ -1, -2 ], [ -1, 2 ], [ -2, -1 ], [ -2, 1 ], + [ 1, -2 ], [ 1, 2 ], [ 2, -1 ], [ 2, 1 ], + ); + + for my $move (@moves) { + my ( $space, $depth ) = $move->@*; + my ( $l, $n ) = split //, $space; + if ( defined $done->{$space} ) { + if ( $done->{$space} eq 'E' ) { + # display_board($board); + return $depth; + } + next; + } + + $board->{$l}{$n} = $depth; + $done->{$space} = $depth; + + for my $jump (@jumps) { + my ( $i, $j ) = $jump->@*; + my $ll = $lets{$l} + $i; + my $nn = $nums{$n} + $j; + + if ( $ll >= 0 && $ll <= 7 ) { + if ( $nn >= 0 && $nn <= 7 ) { + my $new = join '', $stel{$ll}, $smun{$nn}; + push @moves, [ $new, $depth + 1 ]; + } + } + } + } + + # There's always a way, but I don't like not providing an unaccounted + # case + return 'fail'; +} + +sub display_board ($board) { + my @lets = 'a' .. 'h'; + my @nums = reverse 1 .. 8; + for my $num (@nums) { + print qq{$num }; + for my $let (@lets) { + my $char = '_'; + if ( defined $board->{$let}{$num} ) { + $char = + $board->{$let}{$num} == -2 + ? 'E' + : $board->{$let}{$num}; + } + print qq{ [$char] }; + } + say ''; + } + say join ' ', ' ', @lets; +} |
