-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
59 lines (47 loc) · 1.53 KB
/
part2.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
##############
# Read input #
##############
def readInput():
with open('input') as f:
return f.readlines()
# parse input to get a set of rocks
def parse_input(input):
rocks = set()
for line in input:
points = [
tuple(map(int, coords.split(","))) for coords in line.strip().split(" -> ")
]
for ind, point in enumerate(points[:-1:]):
rocks.add(point)
dx, dy = points[ind + 1][0] - point[0], points[ind + 1][1] - point[1]
if dx:
count = 0
step = dx // abs(dx)
while dx != count:
rocks.add((point[0] + count, point[1]))
count += step
if dy:
count = 0
step = dy // abs(dy)
while dy != count:
rocks.add((point[0], point[1] + count))
count += step
rocks.add(points[ind + 1])
return rocks
def part2(input):
rocks = parse_input(input)
y_max = max([y for _, y in rocks])
sand = {(500, 0)}
queue = {(500, 0)}
while queue:
current = queue.pop()
if current[1] >= y_max + 1:
continue
for dx, dy in [(0, 1), (-1, 1), (1, 1)]:
next = (current[0] + dx, current[1] + dy)
if next not in rocks:
sand.add(next)
queue.add(next)
return str(len(sand))
if __name__ == "__main__":
print("Part 2 : " + part2(readInput()))