aboutsummaryrefslogtreecommitdiff
path: root/challenge-281
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-10-14 15:52:47 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-10-14 15:52:47 +0100
commit13976768df8eb3b957bb8b46ce68000fd2f9cc73 (patch)
tree0640ec2505031088dd8bdd17ed6a511f1589832b /challenge-281
parent4a429f814e0628b64b0c8177a3b2344bd4e1d563 (diff)
downloadperlweeklychallenge-club-13976768df8eb3b957bb8b46ce68000fd2f9cc73.tar.gz
perlweeklychallenge-club-13976768df8eb3b957bb8b46ce68000fd2f9cc73.tar.bz2
perlweeklychallenge-club-13976768df8eb3b957bb8b46ce68000fd2f9cc73.zip
Add Python solution to challenge 281
Diffstat (limited to 'challenge-281')
-rw-r--r--challenge-281/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-281/paulo-custodio/python/ch-1.py39
-rw-r--r--challenge-281/paulo-custodio/python/ch-2.py88
3 files changed, 128 insertions, 1 deletions
diff --git a/challenge-281/paulo-custodio/perl/ch-2.pl b/challenge-281/paulo-custodio/perl/ch-2.pl
index 4489f672c3..9464b9a5b2 100644
--- a/challenge-281/paulo-custodio/perl/ch-2.pl
+++ b/challenge-281/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 281
#
-# Task 2: Knight’s Move
+# Task 2: Knight's Move
# Submitted by: Peter Campbell Smith
#
# A Knight in chess can move from its current position to any square two rows
diff --git a/challenge-281/paulo-custodio/python/ch-1.py b/challenge-281/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..8af047ab3f
--- /dev/null
+++ b/challenge-281/paulo-custodio/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env pyhton3
+
+# Challenge 281
+#
+# Task 1: Check Color
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given coordinates, a string that represents the coordinates of a
+# square of the chessboard as shown below:
+#
+# Write a script to return true if the square is light, and false if the square
+# is dark.
+#
+# Example 1
+#
+# Input: $coordinates = "d3"
+# Output: true
+#
+# Example 2
+#
+# Input: $coordinates = "g5"
+# Output: false
+#
+# Example 3
+#
+# Input: $coordinates = "e6"
+# Output: true
+
+import sys
+
+if len(sys.argv) != 2:
+ raise ValueError("Usage: {} coordinates".format(sys.argv[0]))
+
+coords = sys.argv[1]
+if not (len(coords) == 2 and coords[0] in 'abcdefgh' and coords[1] in '12345678'):
+ raise ValueError("Usage: {} coordinates".format(sys.argv[0]))
+
+light = ((ord(coords[0]) - ord('a')) & 1) ^ (int(coords[1]) - 1 & 1)
+print("true" if light else "false")
diff --git a/challenge-281/paulo-custodio/python/ch-2.py b/challenge-281/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..2b4d0a0629
--- /dev/null
+++ b/challenge-281/paulo-custodio/python/ch-2.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+
+# Challenge 281
+#
+# Task 2: Knight's Move
+# Submitted by: Peter Campbell Smith
+#
+# A Knight in chess can move from its current position to any square two rows
+# or columns plus one column or row away. So in the diagram below, if it starts
+# a S, it can move to any of the squares marked E.
+#
+# Write a script which takes a starting position and an ending position and
+# calculates the least number of moves required.
+#
+# Example 1
+#
+# Input: $start = 'g2', $end = 'a8'
+# Ouput: 4
+#
+# g2 -> e3 -> d5 -> c7 -> a8
+#
+# Example 2
+#
+# Input: $start = 'g2', $end = 'h2'
+# Ouput: 3
+#
+# g2 -> e3 -> f1 -> h2
+
+import sys
+
+def find_path_size(start, end):
+ min_path = None
+ paths = [([start], {start: 1})]
+
+ while paths:
+ top = paths.pop(0)
+ path = top[0]
+ seen = top[1]
+
+ # check if we found a solution
+ if path[-1] == end: # found one solution
+ if min_path is None or min_path > len(path):
+ min_path = len(path) # found shorter solution
+
+ # prune the tree
+ if min_path is not None and len(path) > min_path:
+ continue
+
+ # find 8 next positions and push them to paths
+ dest = knight_jump(path[-1])
+ for d in dest:
+ if d not in seen:
+ paths.append([path + [d], {**seen, d: 1}])
+
+ return min_path - 1 # remove start position
+
+def row_col(coord):
+ if not (coord[0] in 'abcdefgh' and coord[1] in '12345678'):
+ raise ValueError("Invalid coordinate")
+ row = 8 - int(coord[1])
+ col = ord(coord[0]) - ord('a')
+ return row, col
+
+def coord(row, col):
+ if row < 0 or row > 7 or col < 0 or col > 7:
+ return None
+ return chr(col + ord('a')) + str(8 - row)
+
+def knight_jump(start):
+ row, col = row_col(start)
+ dest = []
+
+ for r_offset, c_offset in [(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (1, -2), (-1, 2), (1, 2)]:
+ end = coord(row + r_offset, col + c_offset)
+ if end is not None:
+ dest.append(end)
+
+ return dest
+
+if len(sys.argv) != 3:
+ sys.exit("Usage: {} start end".format(sys.argv[0]))
+
+start, end = sys.argv[1], sys.argv[2]
+for pos in (start, end):
+ if not (pos[0] in 'abcdefgh' and pos[1] in '12345678'):
+ sys.exit("Usage: {} start end".format(sys.argv[0]))
+
+print(find_path_size(start, end))