-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem9.py
113 lines (87 loc) · 3.42 KB
/
problem9.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
"""
ADVENT OF CODE 2021
Contestant: Kevin Wood
Problem: 9
"""
import argparse
from math import prod
from typing import List, Set, Tuple
def solve_part1(lines: List[str]) -> int:
cave = Cave([[int(val) for val in line] for line in lines])
low_points = cave.get_low_points()
return sum([cave.risk_level_at(x, y) for (x, y) in low_points])
def solve_part2(lines: List[str]) -> int:
cave = Cave([[int(val) for val in line] for line in lines])
low_points = cave.get_low_points()
basin_sizes = [cave.get_basin_size_at(x, y) for (x, y) in low_points]
basin_sizes.sort(reverse=True)
return prod(basin_sizes[0:3])
class Cave:
def __init__(self, matrix: List[List[int]]) -> None:
self.matrix = matrix
def get_low_points(self) -> List[Tuple[int, int]]:
low_points = [] # type: List[Tuple[int, int]]
for y in range(self.height):
for x in range(self.width):
if self.is_low_point(x, y):
low_points.append((x, y))
return low_points
def get_basin_size_at(self, x: int, y: int) -> int:
checked_points = set() # type: Set[Tuple[int, int]]
points_to_check = {(x, y)}
while len(points_to_check) > 0:
x, y = points_to_check.pop()
checked_points.add((x, y))
neighbors = self.get_open_neighbors_at(x, y)
for nx, ny in neighbors:
if (nx, ny) not in checked_points:
points_to_check.add((nx, ny))
return len(checked_points)
def get_neighbors_at(self, x: int, y: int) -> List[Tuple[int, int]]:
neighbors = [] # type: List[Tuple[int, int]]
if x > 0:
neighbors.append((x - 1, y))
if x < self.width - 1:
neighbors.append((x + 1, y))
if y > 0:
neighbors.append((x, y - 1))
if y < self.height - 1:
neighbors.append((x, y + 1))
return neighbors
def get_open_neighbors_at(self, x: int, y: int) -> List[Tuple[int, int]]:
return [
(nx, ny)
for (nx, ny) in self.get_neighbors_at(x, y)
if self.value_at(nx, ny) != 9
]
def get_neighbor_values(self, x: int, y: int) -> List[int]:
return [self.value_at(nx, ny) for (nx, ny) in self.get_neighbors_at(x, y)]
def is_low_point(self, x: int, y: int) -> bool:
return min(self.get_neighbor_values(x, y)) > self.value_at(x, y)
def value_at(self, x: int, y: int) -> int:
return self.matrix[y][x]
def risk_level_at(self, x: int, y: int) -> int:
return 1 + self.value_at(x, y)
@property
def width(self) -> int:
return len(self.matrix[0])
@property
def height(self) -> int:
return len(self.matrix)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Advent of Code 2021, problem 9")
parser.add_argument(
"-i", "--input_file", help="path to input file", default="input/problem9.txt"
)
parser.add_argument("part", help="part (1|2)", type=int)
args = parser.parse_args()
with open(args.input_file, "r") as file:
lines = [line.strip() for line in file.readlines()]
filtered_lines = list(filter(lambda line: bool(line), lines))
if args.part == 1:
output = solve_part1(filtered_lines)
elif args.part == 2:
output = solve_part2(filtered_lines)
else:
raise ValueError("Unknown part")
print(output)