-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
68 lines (56 loc) · 1.95 KB
/
day14.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
import re
import numpy as np
def line_to_path(line: str):
path = r"(\d+,\d+)"
points = re.findall(path, line)
points = [p.split(",") for p in points]
points = [(int(a), int(b)) for a, b in points]
return list(zip(points[:-1], points[1:]))
def parse_file(fn):
paths = []
with open(fn, "r") as f:
for line in f.readlines():
paths.extend(line_to_path(line))
return paths
def rocks_to_map(rocks, b=False):
x_max, y_max = np.array(rocks).max(axis=1).max(axis=0)
blocked = np.array([[False] * (x_max+1000)] * (y_max+1))
for (start_x, start_y), (end_x, end_y) in rocks:
if (x := start_x) == end_x:
ys = range(start_y, end_y+1, 1) if end_y > start_y else range(end_y, start_y+1, 1)
for y in ys:
blocked[y, x] = True
elif (y := start_y) == end_y:
xs = range(start_x, end_x+1, 1) if end_x > start_x else range(end_x, start_x+1, 1)
for x in xs:
blocked[y, x] = True
else:
raise ValueError()
if b:
blocked = np.concatenate((blocked, np.array([[False] * (x_max+1000)]), np.array([[True] * (x_max+1000)])), axis=0)
return blocked
if __name__ == "__main__":
rocks = parse_file("day14_input.txt")
rocks = rocks_to_map(rocks, True)
np.savetxt("rocks.txt", rocks, fmt="%d")
start = rocks.sum()
landed = True
while landed:
x = 500
if rocks[0, 500]: # part b
break
for y in range(rocks.shape[0]):
if y+1 == rocks.shape[0]:
landed = False
break
if rocks[y+1, x]:
if rocks[y+1, x-1]:
if rocks[y+1, x+1]:
rocks[y, x] = True
break
else:
x += 1
else:
x -= 1
np.savetxt("end.txt", rocks, fmt="%d")
print(rocks.sum()-start)