Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Dark Labyrinth by Sim0000
from collections import Counter
DIR = {"N": (-1, 0), "S": (1, 0), "W": (0, -1), "E": (0, 1)}
m = Counter()
x = 0
y = 0
def find_path(visible):
global x, y # player position for m
# increment visit count
m[(x, y)] += 1
# find player position in visible
xp, yp = next((x, y) for y, row in enumerate(visible) for x, c in enumerate(row) if c == 'P')
# find direction of minimum visit count
v = []
for d in 'NESW':
dy, dx = DIR[d]
if visible[yp + dy][xp + dx] in '.E':
x0, y0 = x + dx, y + dy
if not v or m[(x0, y0)] < v[0]:
v = [m[(x0, y0)], d, x0, y0]
x, y = v[2], v[3] # next position
return v[1] # direction
Oct. 22, 2014
Comments: