diff options
| author | 2021-12-05 23:48:31 +0100 | |
|---|---|---|
| committer | 2021-12-05 23:48:31 +0100 | |
| commit | de73ba3b8ae1831a271a9ae617313aa495f2e48c (patch) | |
| tree | 6fa09064a9f348cd21d93329d1651f6fc612ae49 | |
| parent | b984dc00285148a79e41ac315320e898718762a9 (diff) | |
| download | 2021-de73ba3b8ae1831a271a9ae617313aa495f2e48c.tar.gz 2021-de73ba3b8ae1831a271a9ae617313aa495f2e48c.tar.bz2 2021-de73ba3b8ae1831a271a9ae617313aa495f2e48c.zip | |
Optimized
| -rw-r--r-- | day5/__init__.py | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/day5/__init__.py b/day5/__init__.py index 030df19..0e48b16 100644 --- a/day5/__init__.py +++ b/day5/__init__.py | |||
| @@ -6,6 +6,8 @@ from aoc import BaseAssignment | |||
| 6 | Vector = Tuple[Tuple[int, int], Tuple[int, int]] | 6 | Vector = Tuple[Tuple[int, int], Tuple[int, int]] |
| 7 | 7 | ||
| 8 | class Assignment(BaseAssignment): | 8 | class Assignment(BaseAssignment): |
| 9 | including_diagonals = False | ||
| 10 | |||
| 9 | def parse_item(self, item: str) -> Vector: | 11 | def parse_item(self, item: str) -> Vector: |
| 10 | start, end = item.split('->') | 12 | start, end = item.split('->') |
| 11 | return ( | 13 | return ( |
| @@ -16,6 +18,14 @@ class Assignment(BaseAssignment): | |||
| 16 | def read_input(self, example = False) -> List[Vector]: | 18 | def read_input(self, example = False) -> List[Vector]: |
| 17 | return list(super().read_input(example)) | 19 | return list(super().read_input(example)) |
| 18 | 20 | ||
| 21 | def run(self, input: List[Vector]) -> int: | ||
| 22 | coordinates = [] | ||
| 23 | |||
| 24 | for vector in input: | ||
| 25 | coordinates += points_in_vector(vector, self.including_diagonals) | ||
| 26 | |||
| 27 | return len([c for c in Counter(coordinates).values() if c >= 2]) | ||
| 28 | |||
| 19 | 29 | ||
| 20 | def is_horizontal(vector: Vector): | 30 | def is_horizontal(vector: Vector): |
| 21 | return vector[0][0] == vector[1][0] | 31 | return vector[0][0] == vector[1][0] |
| @@ -70,22 +80,7 @@ def points_in_vector(vector: Vector, includes_diagonals: bool = False) -> List[T | |||
| 70 | class AssignmentOne(Assignment): | 80 | class AssignmentOne(Assignment): |
| 71 | example_result = 5 | 81 | example_result = 5 |
| 72 | 82 | ||
| 73 | def run(self, input: List[Vector]) -> int: | ||
| 74 | coordinates = [] | ||
| 75 | |||
| 76 | for vector in input: | ||
| 77 | coordinates += points_in_vector(vector) | ||
| 78 | |||
| 79 | return len([c for c in Counter(coordinates).values() if c >= 2]) | ||
| 80 | |||
| 81 | 83 | ||
| 82 | class AssignmentTwo(Assignment): | 84 | class AssignmentTwo(Assignment): |
| 83 | example_result = 12 | 85 | example_result = 12 |
| 84 | 86 | including_diagonals = True | |
| 85 | def run(self, input: List[Vector]) -> int: | ||
| 86 | coordinates = [] | ||
| 87 | |||
| 88 | for vector in input: | ||
| 89 | coordinates += points_in_vector(vector, True) | ||
| 90 | |||
| 91 | return len([c for c in Counter(coordinates).values() if c >= 2]) \ No newline at end of file | ||
