diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-05 19:59:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-05 19:59:22 +0100 |
| commit | 0fb8dfe8f0385b42098ec01d0bf5e8d83ce46f4b (patch) | |
| tree | 71e937bdd40298c7fa6c3779f8f0c0ac499722ec | |
| parent | cc32a4510a23266d26a4f02d6dcf3e2b1833a41c (diff) | |
| parent | 39203f1dcd07fc816bcab45f6e3d629887c28d9d (diff) | |
| download | perlweeklychallenge-club-0fb8dfe8f0385b42098ec01d0bf5e8d83ce46f4b.tar.gz perlweeklychallenge-club-0fb8dfe8f0385b42098ec01d0bf5e8d83ce46f4b.tar.bz2 perlweeklychallenge-club-0fb8dfe8f0385b42098ec01d0bf5e8d83ce46f4b.zip | |
Merge pull request #10547 from choroba/ech281
Add solutions to 281: Check Color & Knight's Move by E. Choroba
| -rwxr-xr-x | challenge-281/e-choroba/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-281/e-choroba/perl/ch-2.pl | 43 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-281/e-choroba/perl/ch-1.pl b/challenge-281/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..eb9a98f825 --- /dev/null +++ b/challenge-281/e-choroba/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use builtin qw( true false ); +use experimental qw( signatures ); + +sub check_color($coordinates) { + my ($column, $row) = split //, $coordinates; + return 1 == ($row + ord $column) % 2 +} + +use Test::More tests => 3; + +is check_color('d3'), true, 'Example 1'; +is check_color('g5'), false, 'Example 2'; +is check_color('e6'), true, 'Example 3'; diff --git a/challenge-281/e-choroba/perl/ch-2.pl b/challenge-281/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..60e9bdba29 --- /dev/null +++ b/challenge-281/e-choroba/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub knights_move($start, $end) { + return 0 if $start eq $end; + + tr/a-h/1-8/ for $start, $end; + my %agenda = ($start => undef); + my %visited; + my $tally = 1; + + SEARCH: + while (1) { + my %next; + for my $coord (keys %agenda) { + my ($x, $y) = split //, $coord; + for my $jump ([-2, -1], [-1, -2], [1, -2], [2, -1], + [2, 1], [1, 2], [-1, 2], [-2, 1] + ) { + my ($x2, $y2) = ($x + $jump->[0], $y + $jump->[1]); + next if $x2 < 1 || $y2 < 1 || $x2 > 8 || $y2 > 8; + + last SEARCH if "$x2$y2" eq $end; + + undef $next{"$x2$y2"}; + } + } + %agenda = %next; + } continue { + ++$tally; + } + return $tally +} + +use Test::More tests => 2 + 2; + +is knights_move('g2', 'a8'), 4, 'Example 1'; +is knights_move('g2', 'h2'), 3, 'Example 2'; + +is knights_move('a1', 'a1'), 0, 'Zero'; +is knights_move('a1', 'h8'), 6, 'Far'; |
