summaryrefslogtreecommitdiffstats
path: root/12.py
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-12 09:28:33 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-12 09:28:33 +0000
commit7a71e86dacb03a4ed7dde223b4d720f738c60513 (patch)
tree0758b8a194f30dc54a1734905b2201c9fb7b4615 /12.py
parent6be8c350b242d95894c6ff03548206bd3c185419 (diff)
downloadaoc2022-7a71e86dacb03a4ed7dde223b4d720f738c60513.tar.gz
aoc2022-7a71e86dacb03a4ed7dde223b4d720f738c60513.tar.xz
aoc2022-7a71e86dacb03a4ed7dde223b4d720f738c60513.zip
12
Diffstat (limited to '12.py')
-rw-r--r--12.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/12.py b/12.py
new file mode 100644
index 0000000..ae018b7
--- /dev/null
+++ b/12.py
@@ -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))