diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-06 09:53:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-06 09:53:50 +0100 |
| commit | e8f5304eebb731c60ba892ac315b5373e1d84a4e (patch) | |
| tree | e8b228948db6c3f639c37d066eca796331faf848 | |
| parent | 0a79a91f41e535823a57d6e636816a19a6fe6025 (diff) | |
| parent | 39c5867315fd94c62bdf0bef5767247348b1781e (diff) | |
| download | perlweeklychallenge-club-e8f5304eebb731c60ba892ac315b5373e1d84a4e.tar.gz perlweeklychallenge-club-e8f5304eebb731c60ba892ac315b5373e1d84a4e.tar.bz2 perlweeklychallenge-club-e8f5304eebb731c60ba892ac315b5373e1d84a4e.zip | |
Merge pull request #10553 from kjetillll/challenge-281-kjetillll
Challenge 281 kjetillll
| -rw-r--r-- | challenge-280/kjetillll/perl/ch-2.pl | 3 | ||||
| -rw-r--r-- | challenge-281/kjetillll/perl/ch-1.pl | 6 | ||||
| -rw-r--r-- | challenge-281/kjetillll/perl/ch-2.pl | 26 |
3 files changed, 34 insertions, 1 deletions
diff --git a/challenge-280/kjetillll/perl/ch-2.pl b/challenge-280/kjetillll/perl/ch-2.pl index 904a03eb3f..afc5b87f93 100644 --- a/challenge-280/kjetillll/perl/ch-2.pl +++ b/challenge-280/kjetillll/perl/ch-2.pl @@ -2,7 +2,8 @@ use strict; use warnings; use Test::More tests=>3; sub count { local $_ = shift; - s/ \| .*? \| //x ? count($_) : y/*// + s/ \| .*? \| //xg; + y/*// } is count( $$_{input} ) => $$_{output} diff --git a/challenge-281/kjetillll/perl/ch-1.pl b/challenge-281/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..cae2900422 --- /dev/null +++ b/challenge-281/kjetillll/perl/ch-1.pl @@ -0,0 +1,6 @@ +use strict; use warnings; use Test::More tests=>2; + +sub is_light { pop() =~ /^( [aceg][2468] | [bdfh][1357] )$/x } + +ok is_light( "d3" ) == 1; +ok is_light( "g5" ) == 0; diff --git a/challenge-281/kjetillll/perl/ch-2.pl b/challenge-281/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..6d2836b302 --- /dev/null +++ b/challenge-281/kjetillll/perl/ch-2.pl @@ -0,0 +1,26 @@ +use strict; use warnings; use Test::More; + +sub knight_steps { + #represent the input positions in @_ of the form a1, b2 and so on as + #numeric positions instead where the letters a-h becomes 10 20 30 40 50 60 70 80 + #and concat that with the digit so for example a1 becomes 101, c7 -> 307, h8 -> 808 + + my( $from, $to ) = map s/./ 10*ord($&)-960 /er, @_; #convert input as described + my @check = ( [$from, 0] ); + while( my($pos, $steps) = @{ shift @check } ){ + return $steps if $pos eq $to; + push @check, #push into work list + map [$_, $steps+1], #register new posible jumps + grep /[1-8]0[1-8]/, #don't keep jumps outside board + map $pos + $_, #jump to + 201, 102, 199, 98, #valid jumps + -201, -102, -199, -98; + } +} + +ok knight_steps('g2' => 'a8') == 4; +ok knight_steps('g2' => 'h2') == 3; +ok knight_steps('h1' => 'a8') == 6; +ok knight_steps('c3' => 'c4') == 3; +ok knight_steps('a1' => 'a1') == 0; +done_testing; |
