from collections import defaultdict import astar def parse(file): return [ tuple([int(x) for x in line.strip().split(',')]) for line in file ] def solve(file, N=70, T=1024): falling = parse(file) start = (0,0,0) # x,y,t map = [[0]*N for _ in range(N)] inf = float('inf') map = defaultdict(lambda: inf) for t,p in enumerate(falling): map[p] = t def neighbors(n): x,y,t = n #if T < map[x,y]: # can't move out of a blocked space # return [] def push(x,y,t): if 0 <= x < N and 0 <= y < N: if T <= map[x,y]: out.append((1,(x,y,t))) out = [] push(x+1,y,t+1) push(x,y+1,t+1) push(x-1,y,t+1) push(x,y-1,t+1) return out end = N-1, N-1 def isgoal(n): x,y,_ = n return (x,y) == end sol = astar.search(start, isgoal, neighbors) print(sol) print(len(sol[-1])-1) solve(open("sample1.in"), N=7, T=12) solve(open("input"), N=71, T=1024)