diff options
| -rw-r--r-- | day5/__init__.py | 87 | ||||
| -rw-r--r-- | day5/test_init.py | 14 |
2 files changed, 66 insertions, 35 deletions
diff --git a/day5/__init__.py b/day5/__init__.py index 4d5260d..b16eba7 100644 --- a/day5/__init__.py +++ b/day5/__init__.py | |||
| @@ -19,27 +19,54 @@ class Assignment(BaseAssignment): | |||
| 19 | return list(super().read_input(example)) | 19 | return list(super().read_input(example)) |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | def is_horizontal(v: Vector): | 22 | def is_horizontal(vector: Vector): |
| 23 | return v[0][0] == v[1][0] | 23 | return vector[0][0] == vector[1][0] |
| 24 | 24 | ||
| 25 | def is_vertical(v: Vector): | 25 | def is_vertical(vector: Vector): |
| 26 | return v[0][1] == v[1][1] | 26 | return vector[0][1] == vector[1][1] |
| 27 | 27 | ||
| 28 | def points_in_vector(vector: Vector) -> List[Tuple[int, int]]: | 28 | def is_diagonal(vector: Vector): |
| 29 | first_x = min(vector[0][0], vector[1][0]) | 29 | x_start = vector[0][0] |
| 30 | last_x = max(vector[0][0], vector[1][0]) | 30 | x_end = vector[1][0] |
| 31 | first_y = min(vector[0][1], vector[1][1]) | 31 | y_start = vector[0][1] |
| 32 | last_y = max(vector[0][1], vector[1][1]) | 32 | y_end = vector[1][1] |
| 33 | |||
| 34 | return abs(x_end - x_start) == abs(y_end - y_start) | ||
| 35 | |||
| 36 | def points_in_vector(vector: Vector, includes_diagonals: bool = False) -> List[Tuple[int, int]]: | ||
| 37 | x_start = vector[0][0] | ||
| 38 | x_end = vector[1][0] | ||
| 39 | y_start = vector[0][1] | ||
| 40 | y_end = vector[1][1] | ||
| 41 | |||
| 42 | delta_x = x_end - x_start | ||
| 43 | try: | ||
| 44 | direction_x = int(delta_x / abs(delta_x)) | ||
| 45 | except ZeroDivisionError: | ||
| 46 | direction_x = 0 | ||
| 47 | |||
| 48 | delta_y = y_end - y_start | ||
| 49 | try: | ||
| 50 | direction_y = int(delta_y / abs(delta_y)) | ||
| 51 | except ZeroDivisionError: | ||
| 52 | direction_y = 0 | ||
| 33 | 53 | ||
| 34 | return [ | 54 | return [ |
| 35 | (vector[0][0], y) | 55 | (x_start, y_start + y_delta) |
| 36 | for y | 56 | for y_delta |
| 37 | in range(first_y, last_y + 1) | 57 | in range(0, delta_y + direction_y, direction_y) |
| 38 | ] if is_horizontal(vector) else [ | 58 | ] if is_horizontal(vector) else [ |
| 39 | (x, vector[0][1]) | 59 | (x_start + x_delta, y_start) |
| 40 | for x | 60 | for x_delta |
| 41 | in range(first_x, last_x + 1) | 61 | in range(0, delta_x + direction_x, direction_x) |
| 42 | ] if is_vertical(vector) else [] | 62 | ] if is_vertical(vector) else [ |
| 63 | (x_start + x_delta, y_start + y_delta) | ||
| 64 | for x_delta, y_delta | ||
| 65 | in zip( | ||
| 66 | range(0, delta_x + direction_x, direction_x), | ||
| 67 | range(0, delta_y + direction_y, direction_y) | ||
| 68 | ) | ||
| 69 | ] if includes_diagonals and is_diagonal(vector) else [] | ||
| 43 | 70 | ||
| 44 | 71 | ||
| 45 | class AssignmentOne(Assignment): | 72 | class AssignmentOne(Assignment): |
| @@ -54,21 +81,13 @@ class AssignmentOne(Assignment): | |||
| 54 | return len([c for c in Counter(coordinates).values() if c >= 2]) | 81 | return len([c for c in Counter(coordinates).values() if c >= 2]) |
| 55 | 82 | ||
| 56 | 83 | ||
| 84 | class AssignmentTwo(Assignment): | ||
| 85 | example_result = 12 | ||
| 57 | 86 | ||
| 87 | def run(self, input: List[Vector]) -> int: | ||
| 88 | coordinates = [] | ||
| 89 | |||
| 90 | for vector in input: | ||
| 91 | coordinates += points_in_vector(vector, True) | ||
| 58 | 92 | ||
| 59 | # class AssignmentTwo(Assignment): | 93 | return len([c for c in Counter(coordinates).values() if c >= 2]) \ No newline at end of file |
| 60 | # example_result = 1924 | ||
| 61 | # | ||
| 62 | # def run(self, input: Tuple[List[int], List[BingoCard]]) -> int: | ||
| 63 | # nrs, cards = input | ||
| 64 | # | ||
| 65 | # in_game = copy(cards) | ||
| 66 | # for nr in nrs: | ||
| 67 | # for card in copy(in_game): | ||
| 68 | # card.mark(nr) | ||
| 69 | # | ||
| 70 | # if card.bingo: | ||
| 71 | # in_game.remove(card) | ||
| 72 | # | ||
| 73 | # if len(in_game) == 0: | ||
| 74 | # return nr * sum(card.unmarked) \ No newline at end of file | ||
diff --git a/day5/test_init.py b/day5/test_init.py index 5096a60..a4d7bff 100644 --- a/day5/test_init.py +++ b/day5/test_init.py | |||
| @@ -7,7 +7,19 @@ def test_points_in_vector(): | |||
| 7 | ] | 7 | ] |
| 8 | 8 | ||
| 9 | assert points_in_vector(((9,7), (7,7))) == [ | 9 | assert points_in_vector(((9,7), (7,7))) == [ |
| 10 | (7, 7), (8, 7), (9, 7) | 10 | (9, 7), (8, 7), (7, 7) |
| 11 | ] | ||
| 12 | |||
| 13 | assert points_in_vector(((1,1), (3,3)), False) == [] | ||
| 14 | |||
| 15 | assert points_in_vector(((1,1), (3,3)), True) == [ | ||
| 16 | (1, 1), (2, 2), (3, 3) | ||
| 17 | ] | ||
| 18 | |||
| 19 | assert points_in_vector(((9,7), (7,9)), False) == [] | ||
| 20 | |||
| 21 | assert points_in_vector(((9,7), (7,9)), True) == [ | ||
| 22 | (9, 7), (8, 8), (7, 9), | ||
| 11 | ] | 23 | ] |
| 12 | 24 | ||
| 13 | 25 | ||
