-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem5.py
124 lines (97 loc) · 3.61 KB
/
problem5.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
"""
ADVENT OF CODE 2021
Contestant: Kevin Wood
Problem: 5
"""
import argparse
import attr
import re
from typing import List
def solve(input_lines: List[str], consider_diagonals: bool) -> int:
lines = [Line.from_string(input_line) for input_line in input_lines]
width = height = 0
for line in lines:
width = max(width, line.start.x, line.end.x)
height = max(height, line.start.y, line.end.y)
floor = OceanFloor(width=width + 1, height=height + 1)
for line in lines:
floor.add_line(line, consider_diagonals=consider_diagonals)
return floor.get_num_overlaps()
@attr.s
class Point:
x = attr.ib(type=int)
y = attr.ib(type=int)
@attr.s
class Line:
start = attr.ib(type=Point)
end = attr.ib(type=Point)
@property
def is_horizontal(self) -> bool:
return self.start.x == self.end.x
@property
def is_vertical(self) -> bool:
return self.start.y == self.end.y
@classmethod
def from_string(cls, input_str: str) -> "Line":
parsed = re.match("(\d+),(\d+) -> (\d+),(\d+)", input_str)
if not parsed:
raise ValueError("{} is not a valid line".format(input_str))
parts = [int(v) for v in parsed.groups()]
start = Point(x=parts[0], y=parts[1])
end = Point(x=parts[2], y=parts[3])
return Line(start, end)
class OceanFloor:
def __init__(self, width: int, height: int) -> None:
self.width = width
self.height = height
self.matrix = [[0 for x in range(self.width)] for y in range(self.height)]
def add_line(self, line: Line, consider_diagonals: bool) -> None:
if line.is_horizontal:
for y in range(
min(line.start.y, line.end.y), max(line.start.y, line.end.y) + 1
):
self.inc(line.start.x, y)
elif line.is_vertical:
for x in range(
min(line.start.x, line.end.x), max(line.start.x, line.end.x) + 1
):
self.inc(x, line.start.y)
elif consider_diagonals:
if abs(line.start.x - line.end.x) != abs(line.start.y - line.end.y):
raise ValueError("Lines with slope other than 1 not supported")
x_step = 1 if line.end.x > line.start.x else -1
y_step = 1 if line.end.y > line.start.y else -1
x = line.start.x
y = line.start.y
for _ in range(abs(line.start.x - line.end.x) + 1):
self.inc(x, y)
x += x_step
y += y_step
def get_num_overlaps(self) -> int:
sum = 0
for y in range(self.height):
for x in range(self.width):
if self.value_at(x, y) > 1:
sum += 1
return sum
def value_at(self, x: int, y: int) -> int:
return self.matrix[y][x]
def inc(self, x: int, y: int) -> None:
self.matrix[y][x] += 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Advent of Code 2021, problem 5")
parser.add_argument(
"-i", "--input_file", help="path to input file", default="input/problem5.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(filtered_lines, consider_diagonals=False)
elif args.part == 2:
output = solve(filtered_lines, consider_diagonals=True)
else:
raise ValueError("Unknown part")
print(output)