summaryrefslogtreecommitdiffstats
path: root/12.py
blob: ae018b70b2eb772a7c4c9e9f43a322ac744d3a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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))