mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-22 06:28:11 +01:00
Solve day 5 part 2
This commit is contained in:
parent
6f76176aed
commit
96dc354a2b
2 changed files with 40 additions and 6 deletions
|
@ -37,17 +37,39 @@ class Segment:
|
||||||
def is_vertical(self):
|
def is_vertical(self):
|
||||||
return self.start.x == self.end.x
|
return self.start.x == self.end.x
|
||||||
|
|
||||||
def get_points(self) -> Set[Point]:
|
def get_points_part_1(self) -> Set[Point]:
|
||||||
if self.is_horizontal():
|
if self.is_horizontal():
|
||||||
start = min(self.start.x, self.end.x)
|
start = min(self.start.x, self.end.x)
|
||||||
end = max(self.start.x, self.end.x)
|
end = max(self.start.x, self.end.x)
|
||||||
return {Point(x, self.start.y) for x in range(start, end + 1)}
|
return {Point(x, self.start.y) for x in range(start, end + 1)}
|
||||||
elif self.is_vertical():
|
if self.is_vertical():
|
||||||
start = min(self.start.y, self.end.y)
|
start = min(self.start.y, self.end.y)
|
||||||
end = max(self.start.y, self.end.y)
|
end = max(self.start.y, self.end.y)
|
||||||
return {Point(self.start.x, y) for y in range(start, end + 1)}
|
return {Point(self.start.x, y) for y in range(start, end + 1)}
|
||||||
return set()
|
return set()
|
||||||
|
|
||||||
|
def get_points_part_2(self) -> Set[Point]:
|
||||||
|
part_1 = self.get_points_part_1()
|
||||||
|
if part_1:
|
||||||
|
return part_1
|
||||||
|
|
||||||
|
x = self.start.x
|
||||||
|
y = self.start.y
|
||||||
|
point = Point(x, y)
|
||||||
|
points = {point}
|
||||||
|
while point != self.end:
|
||||||
|
if x < self.end.x:
|
||||||
|
x += 1
|
||||||
|
else:
|
||||||
|
x -= 1
|
||||||
|
if y < self.end.y:
|
||||||
|
y += 1
|
||||||
|
else:
|
||||||
|
y -= 1
|
||||||
|
point = Point(x, y)
|
||||||
|
points.add(point)
|
||||||
|
return points
|
||||||
|
|
||||||
|
|
||||||
def parse_data(data: List[str]) -> List[Segment]:
|
def parse_data(data: List[str]) -> List[Segment]:
|
||||||
segments = []
|
segments = []
|
||||||
|
@ -66,7 +88,7 @@ def solve_part_1(data: List[Segment]) -> int:
|
||||||
seen_points = set()
|
seen_points = set()
|
||||||
multiple_times = set()
|
multiple_times = set()
|
||||||
for segment in data:
|
for segment in data:
|
||||||
for point in segment.get_points():
|
for point in segment.get_points_part_1():
|
||||||
if point in seen_points:
|
if point in seen_points:
|
||||||
multiple_times.add(point)
|
multiple_times.add(point)
|
||||||
seen_points.add(point)
|
seen_points.add(point)
|
||||||
|
@ -74,9 +96,17 @@ def solve_part_1(data: List[Segment]) -> int:
|
||||||
|
|
||||||
|
|
||||||
def solve_part_2(data: List[Segment]) -> int:
|
def solve_part_2(data: List[Segment]) -> int:
|
||||||
return 0
|
seen_points = set()
|
||||||
|
multiple_times = set()
|
||||||
|
for segment in data:
|
||||||
|
for point in segment.get_points_part_2():
|
||||||
|
if point in seen_points:
|
||||||
|
multiple_times.add(point)
|
||||||
|
seen_points.add(point)
|
||||||
|
return len(multiple_times)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main("inputs/day05-test1", expected_part_1=5)
|
main("inputs/day05-test2", expected_part_1=2, expected_part_2=3)
|
||||||
main("inputs/day05", expected_part_1=7297)
|
main("inputs/day05-test1", expected_part_1=5, expected_part_2=12)
|
||||||
|
main("inputs/day05", expected_part_1=7297, expected_part_2=21038)
|
||||||
|
|
4
2021/inputs/day05-test2
Normal file
4
2021/inputs/day05-test2
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1,0 -> 3,0
|
||||||
|
1,0 -> 1,1
|
||||||
|
1,0 -> 2,1
|
||||||
|
2,0 -> 2,1
|
Loading…
Reference in a new issue