Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
15-liner: Generate possible moves of moves @ each count solution in Clear category for Chess Knight by Stensen
from itertools import starmap
DIRS = {(2, -1), (1, 2), (-2, -1), (2, 1), (-1, -2), (-2, 1), (1, -2), (-1, 2)}
def possible_moves(start_x, start_y):
def move_it(coord_x, coord_y):
new_x, new_y = chr(ord(start_x) + coord_x), int(start_y) + coord_y
if 'a' <= new_x <= 'h' and 1 <= new_y <= 8: return f'{new_x}{new_y}'
return set(filter(None, starmap(move_it, DIRS)))
def chess_knight(start, count):
result, stack, i = set(), [start], 0
while i < count:
moves = [j for i in stack for j in possible_moves(*i)]
stack = moves
result.update(moves)
i += 1
return sorted(result)
Nov. 9, 2020