from utils import open_day, Point2D, adjacent_bounded, a_star with open_day(15) as f: inp = [[int(c) for c in l.rstrip()] for l in f] def h(p): return 0 def d(a, b): add = b.x // len(inp[0]) + b.y // len(inp) x, y = b.x % len(inp[0]), b.y % len(inp) return (inp[y][x] + add - 1) % 9 + 1 start = Point2D(0, 0) goal1 = Point2D(len(inp[0]) - 1, len(inp) - 1) neighbours1 = lambda p: adjacent_bounded(p, Point2D(len(inp[0]), len(inp)), False) print(a_star(start, goal1, neighbours1, h, d)) goal2 = Point2D(len(inp[0]) * 5 - 1, len(inp) * 5 - 1) neighbours2 = lambda p: adjacent_bounded(p, Point2D(len(inp[0]) * 5, len(inp) * 5), False) print(a_star(start, goal2, neighbours2, h, d))