-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.py
63 lines (42 loc) · 1.39 KB
/
solve.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
from itertools import pairwise
from pathlib import Path
def parse_traces(lines):
for line in lines:
for start, stop in pairwise(line.split(' -> ')):
start_x, start_y = (int(val) for val in start.split(','))
stop_x, stop_y = (int(val) for val in stop.split(','))
yield sorted((start_x, stop_x)), sorted((start_y, stop_y))
def scan_cave(s):
traces = parse_traces(s.splitlines())
cave = set()
for (x1, x2), (y1, y2) in traces:
cave |= {complex(x, y) for x in range(x1, x2 + 1) for y in range(y1, y2 + 1)}
return cave
def solve(s, part1=False):
cave = scan_cave(s)
hole = 500
abyss = max(pos.imag for pos in cave) + 1
n_grains = 0
grain = hole
while hole not in cave:
for new_pos in (grain + 1j, grain - 1 + 1j, grain + 1 + 1j):
if part1 and (new_pos.imag == abyss):
return n_grains
elif new_pos.imag == abyss + 1:
pass
elif new_pos not in cave:
grain = new_pos
break
else:
cave.add(grain)
n_grains += 1
grain = hole
return n_grains
def part1(s: str):
return solve(s, part1=True)
def part2(s: str):
return solve(s)
if __name__ == '__main__':
DATA = Path(__file__).with_name('data.txt').read_text()
print(part1(DATA))
print(part2(DATA))