-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.py
97 lines (69 loc) · 2.36 KB
/
day18.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Advent of Code 2024, Day 18
# (c) blu3r4y
from queue import PriorityQueue
from aocd.models import Puzzle
from funcy import print_calls, print_durations
from tqdm.auto import tqdm
@print_calls
@print_durations(unit="ms")
def part1(data, length=1024):
coords, bounds = data
walls = set(coords[:length])
return astar_search(walls, 0, bounds, bounds)
@print_calls
@print_durations(unit="ms")
def part2(data):
coords, bounds = data
walls = set()
for xy in tqdm(coords):
if xy in walls:
continue
walls.add(xy)
# check at what point the goal is not reachable anymore
path = astar_search(walls, 0, bounds, bounds)
if not path:
return f"{int(xy.real)},{int(xy.imag)}"
def astar_search(walls, start, goal, bounds):
closed = {start: 0}
openpq = PriorityQueue()
tiebreaker = 0
openpq.put((0, tiebreaker, start))
tiebreaker += 1
while not openpq.empty():
total_steps, _, current = openpq.get()
if current == goal:
return total_steps
for succ in successor_states(current, walls, bounds):
new_steps = closed[current] + 1
if succ not in closed or new_steps < closed[succ]:
closed[succ] = new_steps
estimate = new_steps + manhattan_distance(succ, goal)
openpq.put((estimate, tiebreaker, succ))
tiebreaker += 1
def successor_states(pos, walls, bounds):
for nxt in (pos + step for step in (1, 1j, -1, -1j)):
if within_bounds(nxt, bounds) and nxt not in walls:
yield nxt
def within_bounds(pos, bounds):
return 0 <= pos.real <= bounds.real and 0 <= pos.imag <= bounds.imag
def manhattan_distance(a, b) -> int:
return int(abs(a.imag - b.imag) + abs(a.real - b.real))
def load(data):
coords, width, height = [], 0, 0
for line in data.split("\n"):
x, y = map(int, line.split(","))
coords.append(complex(x, y))
if x > width:
width = x
if y > height:
height = y
bounds = complex(width, height)
return coords, bounds
if __name__ == "__main__":
puzzle = Puzzle(year=2024, day=18)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 326
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == "18,62"
puzzle.answer_b = ans2