-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
133 lines (113 loc) · 3.79 KB
/
solution.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
from pathlib import Path
class Point:
def __init__(self, x=0, y=0, content=" ") -> None:
self.x = x
self.y = y
self.content = content
def getInput(path: str) -> list[str]:
input = Path(path).read_text().splitlines()
return input
def print_scene(occupied: dict):
x_min, y_min = x_max, y_max = list(occupied)[0]
for point in occupied:
x_min = min(x_min, point[0])
x_max = max(x_max, point[0])
y_min = min(y_min, point[1])
y_max = max(y_max, point[1])
for y in range(y_min, y_max + 1):
print(y, end="|")
for x in range(x_min, x_max + 1):
if (x, y) in occupied:
print(occupied[(x, y)].content, end="")
else:
print(" ", end="")
print("")
def parse_walls(input: list[str]) -> dict:
occupied = {}
for line in input:
points = [tuple(map(int, point.split(","))) for point in line.split(" -> ")]
old_point = points[0]
for new_point in points:
x_dir = 1 if old_point[0] <= new_point[0] else -1
y_dir = 1 if old_point[1] <= new_point[1] else -1
for x in range(old_point[0], new_point[0] + x_dir, x_dir):
occupied[(x, new_point[1])] = Point(x, new_point[1], "#")
for y in range(old_point[1], new_point[1] + y_dir, y_dir):
occupied[(new_point[0], y)] = Point(new_point[0], y, "#")
old_point = new_point
return occupied
def fill_sand1(occupied: dict):
y_max = list(occupied)[0][1]
for point in occupied:
y_max = max(y_max, point[1])
source = (500, 0)
done = False
while not done:
grain = Point(source[0], source[1], ".")
settled = False
while not settled:
settled = True
if grain.y + 1 > y_max:
done = True
elif (grain.x, grain.y + 1) not in occupied:
grain.y += 1
settled = False
elif (grain.x - 1, grain.y + 1) not in occupied:
grain.x -= 1
grain.y += 1
settled = False
elif (grain.x + 1, grain.y + 1) not in occupied:
grain.x += 1
grain.y += 1
settled = False
if not done:
occupied[(grain.x, grain.y)] = grain
return
def fill_sand2(occupied: dict):
y_max = list(occupied)[0][1]
for point in occupied:
y_max = max(y_max, point[1])
source = (500, 0)
while source not in occupied:
grain = Point(source[0], source[1], ".")
settled = False
while not settled:
settled = True
if grain.y + 1 == y_max + 2:
settled = True
elif (grain.x, grain.y + 1) not in occupied:
grain.y += 1
settled = False
elif (grain.x - 1, grain.y + 1) not in occupied:
grain.x -= 1
grain.y += 1
settled = False
elif (grain.x + 1, grain.y + 1) not in occupied:
grain.x += 1
grain.y += 1
settled = False
occupied[(grain.x, grain.y)] = grain
return
def part1(input: list[str]) -> str:
occupied = parse_walls(input)
fill_sand1(occupied)
count = 0
for point in occupied:
if occupied[point].content == ".":
count += 1
return count
def part2(input: list[str]) -> str:
occupied = parse_walls(input)
fill_sand2(occupied)
count = 0
for point in occupied:
if occupied[point].content == ".":
count += 1
return count
def main():
input = getInput("input.txt")
print(f"Answer to part 1: {part1(input)}")
print(f"Answer to part 2: {part2(input)}")
return
if __name__ == "__main__":
main()