-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregolith_reservoir.py
146 lines (102 loc) · 2.92 KB
/
regolith_reservoir.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from utils import read_input, run, sliding_window
FNAME = "14/input.txt"
WALL = 1
SAND = 2
def parse_chunk(line):
return [tuple(map(int, coord.split(','))) for coord in line.split(' -> ')]
##########
# PART 1 #
##########
def line_points(start, end):
x1, y1 = start
x2, y2 = end
if x1 == x2:
return [(x1, y) for y in range(min(y1, y2), max(y1, y2) + 1)]
elif y1 == y2:
return [(x, y1) for x in range(min(x1, x2), max(x1, x2) + 1)]
def build_grid(lines):
max_y = max(y for l in lines for _, y in l) + 3
grid = np.zeros((1001, max_y))
for line in lines:
for start, end in sliding_window(line, 2):
for point in line_points(start, end):
grid[point] = WALL
return grid
def try_move(grid, sand):
x, y = sand
next_y = y + 1
if x == 0:
raise Exception(f'{sand}')
if not grid[(x, next_y)]:
return (x, next_y)
if not grid[(x-1, next_y)]:
return (x-1, next_y)
if not grid[(x+1, next_y)]:
return (x+1, next_y)
return sand
def simulate_sand(grid):
current = (500, 0)
if grid[current]:
return False # Full
while True:
next_position = try_move(grid, current)
if next_position == current:
grid[current] = SAND
return True # Landed
if next_position[1] == grid.shape[1] - 1:
return False # Abyss
current = next_position
def part_one(input_file):
grid = build_grid(read_input(input_file, parse_chunk=parse_chunk))
landed = 0
while simulate_sand(grid):
landed += 1
# def next_grid():
# simulate_sand(grid)
# return grid
# show(grid, frames=862, next_grid=next_grid)
return landed
##########
# PART 2 #
##########
def part_two(input_file):
grid = build_grid(read_input(input_file, parse_chunk=parse_chunk))
grid[:, grid.shape[1] - 1] = WALL
landed = 0
while simulate_sand(grid):
landed += 1
# def next_grid():
# for i in range(30):
# simulate_sand(grid)
# return grid
# show(grid, frames=925, next_grid=next_grid)
return landed
def show(grid, frames, next_grid):
def format(grid):
return grid.T[:,350:650]
fig = plt.figure()
im = plt.imshow(format(grid), animated=True, cmap='viridis', vmin=0, vmax=2)
plt.tight_layout()
plt.grid(False)
plt.axis('off')
def init():
im.set_data(format(grid))
return im,
def animate(i):
im.set_array(format(next_grid()))
return im,
anim = animation.FuncAnimation(
fig,
animate,
init_func=init,
frames=frames,
interval=1,
blit=True,
)
anim.save(f"sand_{frames}.gif")
plt.show()
if __name__ == '__main__':
run(part_one, part_two, FNAME)