Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
neighbors generator, sort decorator solution in Clear category for Chess Knight by Phil15
COLS, ROWS = "abcdefgh", "12345678"
MOVES = ((2, -1), (2, 1), (-2, -1), (-2, 1), (-1, 2), (1, 2), (-1, -2), (1, -2))
sort = lambda f: lambda *args: sorted(f(*args))
def neighbors(pos):
c, r = COLS.index(pos[0]), ROWS.index(pos[1])
for col, row in MOVES:
if 0 <= c + col < 8 and 0 <= r + row < 8:
yield COLS[c + col] + ROWS[r + row]
@sort
def chess_knight(start, moves):
if moves==1:
return neighbors(start)
return set.union(*(set(chess_knight(neighbor, moves-1)) | {neighbor}
for neighbor in neighbors(start)))
Sept. 25, 2018
Comments: