diff options
| author | 2021-12-05 23:05:13 +0100 | |
|---|---|---|
| committer | 2021-12-05 23:05:13 +0100 | |
| commit | 372f7ec0a7e8c3358129f200f5b5b74e79ae83ff (patch) | |
| tree | 15b662aefd79ed589bde8cf56018b6ee801327e0 /day5/__init__.py | |
| parent | 5ba784ae1220ec930ffce9ea1ccbb1d6411f841e (diff) | |
| download | 2021-372f7ec0a7e8c3358129f200f5b5b74e79ae83ff.tar.gz 2021-372f7ec0a7e8c3358129f200f5b5b74e79ae83ff.tar.bz2 2021-372f7ec0a7e8c3358129f200f5b5b74e79ae83ff.zip | |
Day 5 part 1
Diffstat (limited to 'day5/__init__.py')
| -rw-r--r-- | day5/__init__.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/day5/__init__.py b/day5/__init__.py new file mode 100644 index 0000000..4d5260d --- /dev/null +++ b/day5/__init__.py | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | from collections import Counter | ||
| 2 | from copy import copy | ||
| 3 | from dataclasses import dataclass | ||
| 4 | from typing import Iterator, List, Optional, Tuple, Callable | ||
| 5 | |||
| 6 | from aoc import BaseAssignment | ||
| 7 | |||
| 8 | Vector = Tuple[Tuple[int, int], Tuple[int, int]] | ||
| 9 | |||
| 10 | class Assignment(BaseAssignment): | ||
| 11 | def parse_item(self, item: str) -> Vector: | ||
| 12 | start, end = item.split('->') | ||
| 13 | return ( | ||
| 14 | tuple(int(_) for _ in start.strip().split(',')), | ||
| 15 | tuple(int(_) for _ in end.strip().split(',')), | ||
| 16 | ) | ||
| 17 | |||
| 18 | def read_input(self, example = False) -> List[Vector]: | ||
| 19 | return list(super().read_input(example)) | ||
| 20 | |||
| 21 | |||
| 22 | def is_horizontal(v: Vector): | ||
| 23 | return v[0][0] == v[1][0] | ||
| 24 | |||
| 25 | def is_vertical(v: Vector): | ||
| 26 | return v[0][1] == v[1][1] | ||
| 27 | |||
| 28 | def points_in_vector(vector: Vector) -> List[Tuple[int, int]]: | ||
| 29 | first_x = min(vector[0][0], vector[1][0]) | ||
| 30 | last_x = max(vector[0][0], vector[1][0]) | ||
| 31 | first_y = min(vector[0][1], vector[1][1]) | ||
| 32 | last_y = max(vector[0][1], vector[1][1]) | ||
| 33 | |||
| 34 | return [ | ||
| 35 | (vector[0][0], y) | ||
| 36 | for y | ||
| 37 | in range(first_y, last_y + 1) | ||
| 38 | ] if is_horizontal(vector) else [ | ||
| 39 | (x, vector[0][1]) | ||
| 40 | for x | ||
| 41 | in range(first_x, last_x + 1) | ||
| 42 | ] if is_vertical(vector) else [] | ||
| 43 | |||
| 44 | |||
| 45 | class AssignmentOne(Assignment): | ||
| 46 | example_result = 5 | ||
| 47 | |||
| 48 | def run(self, input: List[Vector]) -> int: | ||
| 49 | coordinates = [] | ||
| 50 | |||
| 51 | for vector in input: | ||
| 52 | coordinates += points_in_vector(vector) | ||
| 53 | |||
| 54 | return len([c for c in Counter(coordinates).values() if c >= 2]) | ||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | # class AssignmentTwo(Assignment): | ||
| 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 | ||
