Solve 2022 day 12 pt 1-2
BFS, baby. 2 things got me. * S position was not (0,0) in the puzzle input. Such embarrasment to loose time over something so stupid. * I asumed the elevation was fixed to to increase AND DECREASE by 1. From the problem description: > To avoid needing to get out your climbing gear, the elevation of the > destination square can be at most one higher than the elevation of > your current square; that is, if your current elevation is m, you > could step to elevation n, but not to elevation o. _(This also means > that the elevation of the destination square can be much lower than > the elevation of your current square.)_ This means elevation only can _increase_ by one, but _decrease_ with more. So my wrong code: 0 <= abs(n - e) <= 1 got fixed with: n - e <= 1 For pt 2, I reused the loop to find S to collect all "a" positions, and looped them as starting points. Execution time is not the best, but it works.
This commit is contained in:
parent
81c6bc4cd8
commit
92df810943
2 changed files with 64 additions and 0 deletions
|
|
@ -78,6 +78,12 @@ def vdbg(seen, h, w):
|
||||||
print("".join(["#" if (r, c) in seen else " " for c in range(w)]))
|
print("".join(["#" if (r, c) in seen else " " for c in range(w)]))
|
||||||
|
|
||||||
|
|
||||||
|
def vvdbg(seen, h, w):
|
||||||
|
"""Print-debug visited positions of a matrix, with values"""
|
||||||
|
for r in range(h):
|
||||||
|
print("".join([seen[(r, c)] if (r, c) in seen else "." for c in range(w)]))
|
||||||
|
|
||||||
|
|
||||||
def cw(y, x):
|
def cw(y, x):
|
||||||
"""Flip a (y, x) direction counterwise: U->R, R->D, D->L, L->U.
|
"""Flip a (y, x) direction counterwise: U->R, R->D, D->L, L->U.
|
||||||
|
|
||||||
|
|
|
||||||
58
2022-python/output/day_12.py
Normal file
58
2022-python/output/day_12.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import re
|
||||||
|
from collections import deque, Counter, defaultdict
|
||||||
|
from heapq import heappop, heappush
|
||||||
|
from itertools import compress, combinations, chain, permutations
|
||||||
|
|
||||||
|
from output import matrix, D, DDa, vvdbg, DD, ADJ, ints, mhd, mdbg, vdbg, cw, ccw, bk
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
M, H, W = matrix(data)
|
||||||
|
E = S = None
|
||||||
|
p1 = p2 = H * W
|
||||||
|
SP = []
|
||||||
|
for rr in range(H):
|
||||||
|
for cc in range(W):
|
||||||
|
if M[rr][cc] == "E":
|
||||||
|
E = (rr, cc)
|
||||||
|
if M[rr][cc] == "S":
|
||||||
|
S = (rr, cc)
|
||||||
|
SP.append((rr, cc))
|
||||||
|
if M[rr][cc] == "a":
|
||||||
|
SP.append((rr, cc))
|
||||||
|
M = [list(row) for row in M]
|
||||||
|
for rc, v in [(S, "a"), (E, "z")]:
|
||||||
|
r, c = rc
|
||||||
|
M[r][c] = v
|
||||||
|
for sp in SP:
|
||||||
|
Q = [(sp, ord("a"), 0)]
|
||||||
|
seen = set()
|
||||||
|
while Q:
|
||||||
|
yx, e, s = Q.pop(0)
|
||||||
|
if yx in seen:
|
||||||
|
continue
|
||||||
|
seen.add(yx)
|
||||||
|
if yx == E:
|
||||||
|
if sp == S:
|
||||||
|
p1 = s
|
||||||
|
p2 = min(p2, s)
|
||||||
|
y, x = yx
|
||||||
|
for dy, dx in D:
|
||||||
|
if 0 <= y + dy < H and 0 <= x + dx < W:
|
||||||
|
n = ord(M[y + dy][x + dx])
|
||||||
|
if n - e <= 1:
|
||||||
|
Q.append(((y + dy, x + dx), n, s + 1))
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/12.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
|
|
||||||
|
assert p1 == 517
|
||||||
|
assert p2 == 512
|
||||||
Loading…
Add table
Reference in a new issue