diff options
Diffstat (limited to '12.py')
-rw-r--r-- | 12.py | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -0,0 +1,33 @@ +from utils import open_day, Point2D, a_star, adjacent_bounded + +with open_day(12) as f: + inp = [] + for y, l in enumerate(f): + l = l.rstrip() + row = [] + for x, c in enumerate(l): + if c == 'S': + start = Point2D(x, y) + c = 'a' + elif c == 'E': + end = Point2D(x, y) + c = 'z' + row.append(ord(c) - ord('a')) + inp.append(row) + +def h(p): return 0 +def d(a, b): return 1 +def neighbours1(p): + height = inp[p.y][p.x] + for a in adjacent_bounded(p, Point2D(len(inp[0]), len(inp)), False): + adjheight = inp[a.y][a.x] + if adjheight - height <= 1: yield a + +print(a_star(start, end, neighbours1, h, d)) +def neighbours2(p): + height = inp[p.y][p.x] + for a in adjacent_bounded(p, Point2D(len(inp[0]), len(inp)), False): + adjheight = inp[a.y][a.x] + if adjheight - height >= -1: yield a +def goal(p): return inp[p.y][p.x] == 0 +print(a_star(end, goal, neighbours2, h, d)) |