-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
executable file
·85 lines (62 loc) · 2.02 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from itertools import count
from run_util import run_puzzle
D_XY = [(0, 1), (-1, 1), (1, 1)]
def parse_data(data):
lines = [
[
tuple(int(axis) for axis in position.split(','))
for position in line.split(' -> ')
]
for line in data.strip().split('\n')
]
grid = {
(x, y)
for line in lines
for (x1, y1), (x2, y2) in zip(line, line[1:])
for y in range(min(y1, y2), max(y1, y2) + 1)
for x in range(min(x1, x2), max(x1, x2) + 1)
}
return grid
def simulate_sand(grid):
start_position = (500, 0)
lowest_x = max(position[1] for position in grid)
for sand_count in count():
x, y = start_position
while True:
if y > lowest_x: # falling into the endless void
return sand_count
elif (x, y + 1) not in grid: # down one step
x, y = x, y + 1
continue
elif (x - 1, y + 1) not in grid: # one step down and to the left
x, y = x - 1, y + 1
continue
elif (x + 1, y + 1) not in grid: # one step down and to the right
x, y = x + 1, y + 1
continue
else:
grid.add((x, y))
break
if (x, y) == start_position: # source of the sand becomes blocked
return sand_count + 1
def part_a(data):
grid = parse_data(data)
answer = simulate_sand(grid)
return answer
def part_b(data):
grid = parse_data(data)
floor = 2 + max(r[1] for r in grid)
x_min = min(r[0] for r in grid) - floor
x_max = max(r[0] for r in grid) + floor
grid.update((x, floor) for x in range(x_min, x_max))
answer = simulate_sand(grid)
return answer
def main():
examples = [
("""498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9""", 24, 93)
]
day = int(__file__.split('/')[-1].split('.')[0][-2:])
run_puzzle(day, part_a, part_b, examples)
if __name__ == '__main__':
main()