Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Off with her head! solution in Clear category for Snake Lite by veky
class Node:
"mixin class for A*"
heuristic = lambda self, goal: 0 #must be monotonic
def astar(self, goal):
cl, op, parent, kls = set(), {self}, {}, type(self)
g, f = {self: 0}, {self: self.heuristic(goal)}
while op:
t = min(op, key=f.get)
op.remove(t)
if t == goal:
path = []
while t is not self:
t, way = parent[t]
path.append(way)
path.reverse()
return path
cl.add(t)
for u, way, dist in t.neighbors():
if u not in cl:
gu = g[t] + dist
if u not in op or gu < g[u]:
parent[u] = t, way
g[u] = gu
f[u] = gu + u.heuristic(goal)
op.add(u)
def checkio(field_map):
class Cell(Node, tuple):
def neighbors(self):
pos, to = self
t = field.get(pos, "X")
if t == "C": yield Cell((pos, 0)), "", 0
if t in ".0":
for l, n in dict(F=to, L=to*1j, R=to/1j).items():
yield Cell((pos + n, n)), l, 1
field = {}
for i, row in enumerate(field_map):
for j, t in enumerate(row):
z = j - i*1j
if t != "T": field[z] = t
if t == "C": cherry = z
if t == "0": head = z
if t == "1": neck = z
return ''.join(Cell((head, head - neck)).astar(Cell((cherry, 0))))
March 6, 2014
Comments: