Solve 2024:18 p1-2 "RAM Run"
BFS baby. FIRST_BLOCKED was found manually using code from pt 1 to perform a binary search.
This commit is contained in:
parent
f5c3ee938a
commit
9dc3f16766
1 changed files with 90 additions and 0 deletions
90
2024-python/output/day_18.py
Normal file
90
2024-python/output/day_18.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
from collections import deque
|
||||
|
||||
from output import ints, D
|
||||
|
||||
|
||||
def solve(data):
|
||||
grid = [tuple(ints(line)[::-1]) for line in data.splitlines()]
|
||||
sample_size = 1024
|
||||
H = 71
|
||||
W = 71
|
||||
E = H - 1, W - 1
|
||||
p1 = None
|
||||
p2 = None
|
||||
FIRST_BLOCKED = 2974
|
||||
for sample_size in [1024, FIRST_BLOCKED]:
|
||||
answer = None
|
||||
Q = deque([(0, 0, 0)])
|
||||
seen = set()
|
||||
while Q:
|
||||
y, x, w = Q.popleft()
|
||||
if (y, x) in seen:
|
||||
continue
|
||||
seen.add((y, x))
|
||||
if (y, x) == E:
|
||||
answer = w
|
||||
for dy, dx in D:
|
||||
if not (0 <= y + dy < H and 0 <= x + dx < W):
|
||||
continue
|
||||
if (y + dy, x + dx) in grid[:sample_size]:
|
||||
continue
|
||||
Q.append((y + dy, x + dx, w + 1))
|
||||
if not answer:
|
||||
r, c = grid[sample_size - 1]
|
||||
p2 = f"{c},{r}"
|
||||
else:
|
||||
p1 = answer
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
|
||||
# use dummy data
|
||||
inp = """
|
||||
5,4
|
||||
4,2
|
||||
4,5
|
||||
3,0
|
||||
2,1
|
||||
6,3
|
||||
2,4
|
||||
1,5
|
||||
0,6
|
||||
3,3
|
||||
2,6
|
||||
5,1
|
||||
1,2
|
||||
5,5
|
||||
2,5
|
||||
6,5
|
||||
1,4
|
||||
0,4
|
||||
6,4
|
||||
1,1
|
||||
6,1
|
||||
1,0
|
||||
0,5
|
||||
1,6
|
||||
2,0
|
||||
""".strip()
|
||||
|
||||
# uncomment to instead use stdin
|
||||
# import sys; inp = sys.stdin.read().strip()
|
||||
|
||||
# uncomment to use AoC provided puzzle input
|
||||
with open("./input/18.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
# uncomment to do initial data processing shared by part 1-2
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
os.system(f"echo {p1} | wl-copy")
|
||||
print(p2)
|
||||
os.system(f"echo {p2} | wl-copy")
|
||||
|
||||
# uncomment and replace 0 with actual output to refactor code
|
||||
# and ensure nonbreaking changes
|
||||
# assert p1 == 0
|
||||
# assert p2 == 0
|
||||
Loading…
Add table
Reference in a new issue