Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursion solution in Clear category for Stepping Stones Puzzle by kdim
def stepping_stones(n: int, ones: list[tuple[int, int]]) -> int:
def walk(board):
m = max(board.values()) + 1
s = lambda x, y: sum(board.get((i, j), 0) for i in (x - 1, x, x + 1) # lambda - get sum around cell (x, y)
for j in (y - 1, y, y + 1)) #
cells = {(x, y) for i, j in board.keys() # cells - get cells around black stones
for x in (i - 1, i, i + 1) # where we can put a white stone
for y in (j - 1, j, j + 1) #
if (x, y) not in board and x > -1 < y < n > x and m == s(x, y)} #
return max([walk(board | {cell: m}) for cell in cells], default=m - 1) # recursion - walk for every cell
#
board = {(i, j): 1 for i, j in ones} # board - as dict
return walk(board)
Feb. 22, 2023