aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-278/sgreen/README.md3
-rw-r--r--challenge-279/sgreen/README.md3
-rw-r--r--challenge-280/sgreen/README.md3
-rw-r--r--challenge-281/sgreen/README.md4
-rw-r--r--challenge-281/sgreen/blog.txt1
-rwxr-xr-xchallenge-281/sgreen/perl/ch-1.pl32
-rwxr-xr-xchallenge-281/sgreen/perl/ch-2.pl86
-rwxr-xr-xchallenge-281/sgreen/python/ch-1.py27
-rwxr-xr-xchallenge-281/sgreen/python/ch-2.py69
-rwxr-xr-xchallenge-281/sgreen/python/test.py20
10 files changed, 237 insertions, 11 deletions
diff --git a/challenge-278/sgreen/README.md b/challenge-278/sgreen/README.md
deleted file mode 100644
index 8bd32e6a72..0000000000
--- a/challenge-278/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 277
-
-Blog: [Strong counting](https://dev.to/simongreennet/strong-counting-10on)
diff --git a/challenge-279/sgreen/README.md b/challenge-279/sgreen/README.md
deleted file mode 100644
index 8bd32e6a72..0000000000
--- a/challenge-279/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 277
-
-Blog: [Strong counting](https://dev.to/simongreennet/strong-counting-10on)
diff --git a/challenge-280/sgreen/README.md b/challenge-280/sgreen/README.md
deleted file mode 100644
index 8bd32e6a72..0000000000
--- a/challenge-280/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 277
-
-Blog: [Strong counting](https://dev.to/simongreennet/strong-counting-10on)
diff --git a/challenge-281/sgreen/README.md b/challenge-281/sgreen/README.md
index 8bd32e6a72..5716a00b5c 100644
--- a/challenge-281/sgreen/README.md
+++ b/challenge-281/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 277
+# The Weekly Challenge 281
-Blog: [Strong counting](https://dev.to/simongreennet/strong-counting-10on)
+Blog: [The one about a chess board](https://dev.to/simongreennet/the-one-about-a-chess-board-57mh)
diff --git a/challenge-281/sgreen/blog.txt b/challenge-281/sgreen/blog.txt
new file mode 100644
index 0000000000..b13530f392
--- /dev/null
+++ b/challenge-281/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-one-about-a-chess-board-57mh \ No newline at end of file
diff --git a/challenge-281/sgreen/perl/ch-1.pl b/challenge-281/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..e4e3aaf76c
--- /dev/null
+++ b/challenge-281/sgreen/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+
+sub main ($coords) {
+ # Determine if a coordinate on a chess board is light
+
+ # Check if the position is valid
+ if ( $coords !~ /^[a-h][1-8]$/ ) {
+ die "Not a valid chess coordinate!\n";
+ }
+
+ my $dark_first = any { substr($coords, 0, 1) eq $_ } ('a','c','e','g');
+
+ if ($dark_first and substr($coords,1,1) % 2 == 0) {
+ say 'true';
+ return;
+ }
+ if (not $dark_first and substr($coords,1,1) % 2 == 1) {
+ say 'true';
+ return;
+ }
+
+ say 'false';
+}
+
+main($ARGV[0]); \ No newline at end of file
diff --git a/challenge-281/sgreen/perl/ch-2.pl b/challenge-281/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..4c9e7f6c25
--- /dev/null
+++ b/challenge-281/sgreen/perl/ch-2.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+
+sub convert_coord_to_list($coord) {
+ my @letters = ( undef, 'a' .. 'h' );
+ # 'a' is 97 in the ascii table
+ return [ ord(substr( $coord, 0, 1 )) - 96, substr( $coord, 1, 1 ) ];
+}
+
+sub coord_equals ( $c1, $c2 ) {
+ return $c1->[0] == $c2->[0] && $c1->[1] == $c2->[1];
+}
+
+sub main ( $start_coord, $end_coord ) {
+ # Calculate the least number of knight moves between two positions
+
+ # Check if the position is valid
+ foreach my $coord ( $start_coord, $end_coord ) {
+ if ( $coord !~ /^[a-h][1-8]$/ ) {
+ die "The position $coord is not a valid chess coordinate!\n";
+ }
+ }
+
+ # Direction the knight piece can move
+ my @deltas = (
+ [ 2, 1 ], [ 2, -1 ], [ -2, 1 ], [ -2, -1 ],
+ [ 1, 2 ], [ 1, -2 ], [ -1, 2 ], [ -1, -2 ]
+ );
+
+ # Where we start
+ my @coords = ( convert_coord_to_list($start_coord) );
+
+ # Where we want to end
+ my $target = convert_coord_to_list($end_coord);
+
+ # Count the required moves
+ my $moves = 1;
+
+ # Co-ordinates we've already been to
+ my @seen = ();
+
+ while (1) {
+ # The new coordinates after we've made the next move
+ my @new_coords = ();
+
+ # For all existing places after the previous move
+ foreach my $coord (@coords) {
+ # Move the knight in all possible ways
+ foreach my $delta (@deltas) {
+ my $new_pos =
+ [ $coord->[0] + $delta->[0], $coord->[1] + $delta->[1] ];
+
+ # But exclude moves that take it off the board or we've already used
+ if ( $new_pos->[0] < 1
+ or $new_pos->[0] > 8
+ or $new_pos->[1] < 1
+ or $new_pos->[1] > 8
+ or any { coord_equals( $new_pos, $_ ) } @seen )
+ {
+ next;
+ }
+
+ if ( coord_equals( $new_pos, $target ) ) {
+ # We've hit the target position
+ say $moves;
+ return;
+ }
+
+ push @new_coords, $new_pos;
+ push @seen, $new_pos;
+ }
+ }
+
+ # Looks like we'll need to move again!
+ @coords = @new_coords;
+ $moves++;
+ }
+}
+
+main( $ARGV[0], $ARGV[1] ); \ No newline at end of file
diff --git a/challenge-281/sgreen/python/ch-1.py b/challenge-281/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..293f65177e
--- /dev/null
+++ b/challenge-281/sgreen/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def check_color(coords: str) -> bool:
+ '''Determine if a coordinate on a chess board is light'''
+
+ # Check if the position is valid
+ if not re.search('^[a-h][1-8]$', coords):
+ raise ValueError('Not a valid chess coordinate!')
+
+ if coords[0] in ('a', 'c', 'e', 'g') and int(coords[1]) % 2 == 0:
+ return True
+ if coords[0] in ('b', 'd', 'f', 'h') and int(coords[1]) % 2 == 1:
+ return True
+ return False
+
+
+def main():
+ result = check_color(sys.argv[1])
+ print('true' if result else 'false')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-281/sgreen/python/ch-2.py b/challenge-281/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..76246733aa
--- /dev/null
+++ b/challenge-281/sgreen/python/ch-2.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def convert_coord_to_list(coord: str) -> list:
+ letters = ' abcdefgh'
+ return (letters.index(coord[0]), int(coord[1]))
+
+
+def knights_move(start_coord: str, end_coord: str) -> int:
+ '''Calculate the least number of knight moves between two positions'''
+
+ # Check if the position is valid
+ for coord in (start_coord, end_coord):
+ if not re.search('^[a-h][1-8]$', coord):
+ raise ValueError(
+ f'The position {coord} is not a valid chess coordinate!')
+
+ # Direction the knight piece can move
+ deltas = ([2, 1], [2, -1], [-2, 1], [-2, -1],
+ [1, 2], [1, -2], [-1, 2], [-1, -2])
+
+ # Where we start
+ coords = [convert_coord_to_list(start_coord)]
+
+ # Where we want to end
+ target = convert_coord_to_list(end_coord)
+
+ # Count the required moves
+ moves = 1
+
+ # Co-ordinates we've already been to
+ seen = []
+
+ while True:
+ # The new coordinates after we've made the next move
+ new_coords = []
+
+ # For all existing places after the previous move
+ for coord in coords:
+ # Move the knight in all possible ways
+ for delta in deltas:
+ new_pos = (coord[0] + delta[0], coord[1] + delta[1])
+
+ # But exclude moves that take it off the board or we've already used
+ if not 0 < new_pos[0] < 9 or not 0 < new_pos[1] < 9 or new_pos in seen:
+ continue
+
+ if new_pos == target:
+ # We've hit the target position
+ return moves
+
+ new_coords.append(new_pos)
+ seen.append(new_pos)
+
+ # Looks like we'll need to move again!
+ coords = new_coords
+ moves += 1
+
+
+def main():
+ result = knights_move(sys.argv[1], sys.argv[2])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-281/sgreen/python/test.py b/challenge-281/sgreen/python/test.py
new file mode 100755
index 0000000000..a385705bf1
--- /dev/null
+++ b/challenge-281/sgreen/python/test.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertTrue(ch_1.check_color('d3'))
+ self.assertFalse(ch_1.check_color('g5'))
+ self.assertTrue(ch_1.check_color('e6'))
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.knights_move('g2', 'a8'), 4)
+ self.assertEqual(ch_2.knights_move('g2', 'h2'), 3)
+
+
+if __name__ == '__main__':
+ unittest.main()