import astar def solve(file): map = [line.strip() for line in file] for y in range(len(map)): for x,c in enumerate(map[y]): if c == 'S': start = (x,y,'>') elif c == 'E': end = (x,y) def isgoal(n): x,y,_ = n #print("isgoal", n) return (x,y) == end def get(x,y): return map[y][x] print(map) print(start, end) def neighbors(src): x,y,dir = src n = [] def push(x,y, newdir): if 0 <= y < len(map): if 0 <= x < len(map[y]): if get(x,y) != '#': cost = 1 if dir == '.' or newdir == dir else 1001 n.append((cost, (x,y,newdir))) push(x-1, y, '<') push(x+1,y, '>') push(x, y-1, '^') push(x, y+1, 'v') return n cost, _, nodes = astar.search(start, isgoal, neighbors) print(cost) print(len(set((x,y) for x,y,_ in nodes))) solve(open('sample1.in')) solve(open('input'))