aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetilS <kjetilskotheim@gmail.com>2024-08-06 01:34:57 +0200
committerKjetilS <kjetilskotheim@gmail.com>2024-08-06 01:34:57 +0200
commitb029a5cccde12c8c49738508cfa0d003e5856adc (patch)
treeae392f63a3036e158274dc31caa6961ba7f2a3ea
parent7aced3260b50bc903ed5cb12cf5a98af98e00861 (diff)
downloadperlweeklychallenge-club-b029a5cccde12c8c49738508cfa0d003e5856adc.tar.gz
perlweeklychallenge-club-b029a5cccde12c8c49738508cfa0d003e5856adc.tar.bz2
perlweeklychallenge-club-b029a5cccde12c8c49738508cfa0d003e5856adc.zip
https://theweeklychallenge.org/blog/perl-weekly-challenge-281/
-rw-r--r--challenge-281/kjetillll/perl/ch-1.pl6
-rw-r--r--challenge-281/kjetillll/perl/ch-2.pl26
2 files changed, 32 insertions, 0 deletions
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;