diff options
| -rw-r--r-- | conftest.py | 4 | ||||
| -rw-r--r-- | day12/__init__.py | 5 | ||||
| -rw-r--r-- | day13/__init__.py | 109 | ||||
| -rw-r--r-- | day13/example.txt | 21 | ||||
| -rw-r--r-- | day13/input.txt | 996 |
5 files changed, 1127 insertions, 8 deletions
diff --git a/conftest.py b/conftest.py index c846afa..24884e2 100644 --- a/conftest.py +++ b/conftest.py | |||
| @@ -21,14 +21,12 @@ def pytest_generate_tests(metafunc: Metafunc): | |||
| 21 | for part in ['1', '2'] | 21 | for part in ['1', '2'] |
| 22 | ] | 22 | ] |
| 23 | 23 | ||
| 24 | print(assignments) | ||
| 25 | |||
| 26 | metafunc.parametrize( | 24 | metafunc.parametrize( |
| 27 | argnames=f'assignment', | 25 | argnames=f'assignment', |
| 28 | argvalues=[ | 26 | argvalues=[ |
| 29 | Assignment(path=package.__path__[0]) | 27 | Assignment(path=package.__path__[0]) |
| 30 | for (Assignment, package) in assignments | 28 | for (Assignment, package) in assignments |
| 31 | if Assignment is not None and hasattr(package, '__path__') | 29 | if Assignment is not None and hasattr(package, '__path__') and Assignment.example_result != NotImplemented |
| 32 | ], | 30 | ], |
| 33 | ids=lambda assignment: str(assignment) | 31 | ids=lambda assignment: str(assignment) |
| 34 | ) | 32 | ) |
diff --git a/day12/__init__.py b/day12/__init__.py index aad531d..9d7a036 100644 --- a/day12/__init__.py +++ b/day12/__init__.py | |||
| @@ -108,9 +108,4 @@ class AssignmentTwo(Assignment): | |||
| 108 | 108 | ||
| 109 | def run(self, input: Dict[str, Node]) -> int: | 109 | def run(self, input: Dict[str, Node]) -> int: |
| 110 | paths = sorted(self.calculate_all_paths(input['start'], input['end'])) | 110 | paths = sorted(self.calculate_all_paths(input['start'], input['end'])) |
| 111 | |||
| 112 | print() | ||
| 113 | for path in paths: | ||
| 114 | print(','.join([ n.id for n in path ])) | ||
| 115 | |||
| 116 | return len(paths) | 111 | return len(paths) |
diff --git a/day13/__init__.py b/day13/__init__.py new file mode 100644 index 0000000..41b81dc --- /dev/null +++ b/day13/__init__.py | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | import re | ||
| 2 | from abc import ABC | ||
| 3 | from copy import copy | ||
| 4 | from dataclasses import dataclass | ||
| 5 | from typing import List, Tuple, Union, TypedDict | ||
| 6 | |||
| 7 | from aoc import BaseAssignment | ||
| 8 | |||
| 9 | Coordinate = Tuple[int, int] | ||
| 10 | |||
| 11 | def mirrored_index(index: int, fold: int) -> int: | ||
| 12 | return fold + (fold - index) | ||
| 13 | |||
| 14 | @dataclass | ||
| 15 | class Sheet: | ||
| 16 | dots: List[Coordinate] | ||
| 17 | width: int = 0 | ||
| 18 | height: int = 0 | ||
| 19 | |||
| 20 | def __post_init__(self): | ||
| 21 | x_coordinates, y_coordinates = zip(*self.dots) | ||
| 22 | |||
| 23 | self.width = max(x_coordinates) + 1 | ||
| 24 | self.height = max(y_coordinates) + 1 | ||
| 25 | |||
| 26 | |||
| 27 | def fold_x(self, position: int): | ||
| 28 | for x, y in copy(self.dots): | ||
| 29 | mirrored_x = mirrored_index(x, position) | ||
| 30 | if mirrored_x < position: | ||
| 31 | self.dots.append((mirrored_x, y)) | ||
| 32 | self.dots.remove((x, y)) | ||
| 33 | |||
| 34 | self.width = self.width // 2 | ||
| 35 | self.dots = list(set(self.dots)) | ||
| 36 | |||
| 37 | def fold_y(self, position: int): | ||
| 38 | for x, y in copy(self.dots): | ||
| 39 | mirrored_y = mirrored_index(y, position) | ||
| 40 | if mirrored_y < position: | ||
| 41 | self.dots.append((x, mirrored_y)) | ||
| 42 | self.dots.remove((x, y)) | ||
| 43 | |||
| 44 | self.height = self.height // 2 | ||
| 45 | self.dots = list(set(self.dots)) | ||
| 46 | |||
| 47 | def __repr__(self): | ||
| 48 | dots = set(self.dots) | ||
| 49 | return '\n'.join([ | ||
| 50 | '', | ||
| 51 | *[ | ||
| 52 | ''.join([ | ||
| 53 | '█' if (x, y) in dots else ' ' | ||
| 54 | for x in range(self.width) | ||
| 55 | ]) | ||
| 56 | for y in range(self.height) | ||
| 57 | ], | ||
| 58 | '', | ||
| 59 | ]) | ||
| 60 | |||
| 61 | class Instruction(TypedDict): | ||
| 62 | direction: str | ||
| 63 | position: int | ||
| 64 | |||
| 65 | coordinate_regex = re.compile('(\d+),(\d+)') | ||
| 66 | instruction_regex = re.compile('fold along (?P<direction>[xy])=(?P<position>\d+)') | ||
| 67 | |||
| 68 | class Assignment(BaseAssignment, ABC): | ||
| 69 | def parse_item(self, item: str) -> Union[Coordinate,Instruction]: | ||
| 70 | if coordinate := coordinate_regex.match(item): | ||
| 71 | return tuple(int(i) for i in coordinate.groups()) | ||
| 72 | |||
| 73 | if instruction := instruction_regex.match(item): | ||
| 74 | return { | ||
| 75 | **instruction.groupdict(), | ||
| 76 | 'position': int(instruction.groupdict()['position']) | ||
| 77 | } | ||
| 78 | |||
| 79 | def read_input(self, example = False) -> Tuple[Sheet, List[Instruction]]: | ||
| 80 | input = super().read_input(example) | ||
| 81 | |||
| 82 | coordinates: List[Coordinate] = [] | ||
| 83 | |||
| 84 | while True: | ||
| 85 | coordinate = next(input) | ||
| 86 | if coordinate is None: | ||
| 87 | break | ||
| 88 | |||
| 89 | coordinates.append(coordinate) | ||
| 90 | |||
| 91 | sheet = Sheet(dots=coordinates) | ||
| 92 | |||
| 93 | return sheet, list(input) | ||
| 94 | |||
| 95 | |||
| 96 | class AssignmentOne(Assignment): | ||
| 97 | example_result = 17 | ||
| 98 | |||
| 99 | def run(self, input: Tuple[Sheet, List[Instruction]]) -> int: | ||
| 100 | sheet, instructions = input | ||
| 101 | getattr(sheet, f'fold_{instructions[0]["direction"]}')(instructions[0]["position"]) | ||
| 102 | return len(sheet.dots) | ||
| 103 | |||
| 104 | class AssignmentTwo(Assignment): | ||
| 105 | def run(self, input: Tuple[Sheet, List[Instruction]]) -> Sheet: | ||
| 106 | sheet, instructions = input | ||
| 107 | for instruction in instructions: | ||
| 108 | getattr(sheet, f'fold_{instruction["direction"]}')(instruction["position"]) | ||
| 109 | return sheet | ||
diff --git a/day13/example.txt b/day13/example.txt new file mode 100644 index 0000000..282114c --- /dev/null +++ b/day13/example.txt | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | 6,10 | ||
| 2 | 0,14 | ||
| 3 | 9,10 | ||
| 4 | 0,3 | ||
| 5 | 10,4 | ||
| 6 | 4,11 | ||
| 7 | 6,0 | ||
| 8 | 6,12 | ||
| 9 | 4,1 | ||
| 10 | 0,13 | ||
| 11 | 10,12 | ||
| 12 | 3,4 | ||
| 13 | 3,0 | ||
| 14 | 8,4 | ||
| 15 | 1,10 | ||
| 16 | 2,14 | ||
| 17 | 8,10 | ||
| 18 | 9,0 | ||
| 19 | |||
| 20 | fold along y=7 | ||
| 21 | fold along x=5 | ||
diff --git a/day13/input.txt b/day13/input.txt new file mode 100644 index 0000000..0c5500b --- /dev/null +++ b/day13/input.txt | |||
| @@ -0,0 +1,996 @@ | |||
| 1 | 381,143 | ||
| 2 | 954,841 | ||
| 3 | 1061,183 | ||
| 4 | 654,247 | ||
| 5 | 30,192 | ||
| 6 | 72,215 | ||
| 7 | 1223,680 | ||
| 8 | 512,367 | ||
| 9 | 150,747 | ||
| 10 | 85,890 | ||
| 11 | 495,127 | ||
| 12 | 714,710 | ||
| 13 | 31,238 | ||
| 14 | 242,878 | ||
| 15 | 1218,772 | ||
| 16 | 862,654 | ||
| 17 | 1190,710 | ||
| 18 | 112,229 | ||
| 19 | 276,679 | ||
| 20 | 1225,240 | ||
| 21 | 1104,654 | ||
| 22 | 1120,175 | ||
| 23 | 266,690 | ||
| 24 | 142,79 | ||
| 25 | 977,639 | ||
| 26 | 626,203 | ||
| 27 | 1134,147 | ||
| 28 | 898,206 | ||
| 29 | 438,731 | ||
| 30 | 75,800 | ||
| 31 | 731,576 | ||
| 32 | 932,287 | ||
| 33 | 283,236 | ||
| 34 | 659,222 | ||
| 35 | 241,751 | ||
| 36 | 682,182 | ||
| 37 | 305,612 | ||
| 38 | 651,218 | ||
| 39 | 152,491 | ||
| 40 | 890,100 | ||
| 41 | 788,823 | ||
| 42 | 1198,229 | ||
| 43 | 842,189 | ||
| 44 | 1283,789 | ||
| 45 | 887,113 | ||
| 46 | 17,679 | ||
| 47 | 92,122 | ||
| 48 | 831,21 | ||
| 49 | 894,637 | ||
| 50 | 803,52 | ||
| 51 | 1235,36 | ||
| 52 | 373,669 | ||
| 53 | 512,444 | ||
| 54 | 864,376 | ||
| 55 | 184,316 | ||
| 56 | 104,822 | ||
| 57 | 303,340 | ||
| 58 | 82,289 | ||
| 59 | 465,878 | ||
| 60 | 890,794 | ||
| 61 | 1041,29 | ||
| 62 | 925,116 | ||
| 63 | 847,553 | ||
| 64 | 905,330 | ||
| 65 | 296,382 | ||
| 66 | 708,387 | ||
| 67 | 1255,718 | ||
| 68 | 530,669 | ||
| 69 | 977,460 | ||
| 70 | 1153,568 | ||
| 71 | 30,30 | ||
| 72 | 350,740 | ||
| 73 | 872,781 | ||
| 74 | 944,893 | ||
| 75 | 1069,143 | ||
| 76 | 438,427 | ||
| 77 | 1222,425 | ||
| 78 | 925,555 | ||
| 79 | 1034,215 | ||
| 80 | 547,379 | ||
| 81 | 1233,567 | ||
| 82 | 982,373 | ||
