-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
88 lines (69 loc) · 2.27 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
from stocking import clockit
from collections import deque
# INPUT, MAX_IDX, FIRST_DROP_COUNT = 'test1', 6, 12
INPUT, MAX_IDX, FIRST_DROP_COUNT = 'input', 70, 1024
size = MAX_IDX + 1
def ram_run():
incoming = [parse(line) for line in lines()]
ram = Ram(size)
end = (MAX_IDX, MAX_IDX)
for coord in incoming[:FIRST_DROP_COUNT]:
ram[coord] = '#'
history = navigate(ram)
ans1 = history[end]
print(f'Part 1: {ans1}')
low, high = FIRST_DROP_COUNT, len(incoming)
while low < high:
mid = (low + high) // 2
ram = Ram(size)
for coord in incoming[:mid]:
ram[coord] = '#'
history = navigate(ram)
if end in history:
low = mid + 1
else:
high = mid
ans2 = ','.join(str(n) for n in incoming[mid - 1])
print(f'Part 2: {ans2}')
def navigate(ram):
history = {(0, 0): 0}
explore_around = deque([((0, 0), 0)])
while explore_around:
pos, steps = explore_around.popleft()
steps += 1
for coord, value in ram.adjacent(pos):
if value != '#' and (coord not in history or steps < history[coord]):
history[coord] = steps
explore_around.append((coord, steps))
return history
def parse(line):
parts = line.split(',')
return int(parts[0]), int(parts[1])
def lines():
return open(f'./input/2024/day18/{INPUT}.txt').read().strip().splitlines()
class Ram:
adjacent_directions = (0, -1), (1, 0), (0, 1), (-1, 0)
def __init__(self, size):
self.rows = [['.'] * size for _ in range(size)]
self.height = size
self.width = size
def __setitem__(self, key, value):
self.rows[key[1]][key[0]] = value
def get(self, coord):
x, y = coord
if 0 <= y < self.height and 0 <= x < self.width:
return self.rows[y][x]
else:
return '#'
def next(self, coord, direction):
x, y = coord
dx, dy = direction
return (x := x + dx, y := y + dy), self.get((x, y))
def adjacent(self, coord):
x, y = coord
return [
coord_item
for direction in self.adjacent_directions
if (coord_item := self.next(coord, direction))
is not None]
clockit(ram_run)