diff options
Diffstat (limited to 'challenge-281/roger-bell-west/python/ch-2.py')
| -rwxr-xr-x | challenge-281/roger-bell-west/python/ch-2.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-281/roger-bell-west/python/ch-2.py b/challenge-281/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..e6bbe85da8 --- /dev/null +++ b/challenge-281/roger-bell-west/python/ch-2.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 + +from collections import deque + +def cs2xy(a): + x = ord(a[0]) - ord('a') + y = ord(a[1]) - ord('1') + return [x, y] + +def knightsmove(ffrom, to): + fc = cs2xy(ffrom) + tc = cs2xy(to) + queue = deque() + queue.append((fc[0], fc[1], 0)) + seen = set() + while len(queue) > 0: + cc = queue.popleft() + if cc[0] == tc[0] and cc[1] == tc[1]: + return cc[2] + else: + for offset in ( (2, 1), (1, 2), (2, -1), (1, -2), (-2, 1), (-1, 2), (-2, -1), (-1, -2) ): + x = cc[0] + offset[0] + y = cc[1] + offset[1] + if x >= 0 and x <= 7 and y >= 0 and y <= 7: + cv = x * 8 + y + if cv not in seen: + queue.append((x, y, cc[2] + 1)) + seen.add(cv) + return -1 + +import unittest + +class TestKnightsmove(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(knightsmove("g2", "a8"), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(knightsmove("g2", "h2"), 3, 'example 2') + +unittest.main() |
