Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14-liner: simple complex solution in Clear category for Dark Labyrinth by przemyslaw.daniel
def find_path(visible):
c_visible = lambda i: visible[int(i.real)][int(i.imag)]
neighbors = {"S": 1, "N": -1, "E": 1j, "W": -1j}
if 'path' not in dir(find_path): find_path.path = ''
height, width, visited = len(visible), len(visible[0]), dict()
a = b = [x+1j*y for x, y in __import__('itertools').product(
range(height), range(width)) if visible[x][y] == 'P'][0]
for d in find_path.path[::-1]:
a -= neighbors[d]
visited[a] = visited.get(a, 0) + 1
move = [(k, v+b) for k, v in neighbors.items()
if abs(v+b) < abs(height+1j*width) and c_visible(v+b) in '.E']
find_path.path += min(move, key=lambda k: visited.get(k[1], 0))[0]
return find_path.path[-1]
March 9, 2017
Comments: