diff options
| author | 2022-12-17 17:17:54 +0100 | |
|---|---|---|
| committer | 2022-12-17 17:17:54 +0100 | |
| commit | 5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d (patch) | |
| tree | 7bfa9fa0971d7311272ea6f234c223093e4e9615 | |
| parent | 2d1263453d26cd2c6a64a3b6141ee0a12ddc0b11 (diff) | |
| download | 2022-5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d.tar.gz 2022-5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d.tar.bz2 2022-5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d.zip | |
Added some utilities
| -rw-r--r-- | aoc/datastructures.py | 27 | ||||
| -rw-r--r-- | aoc/decorators.py | 17 | ||||
| -rw-r--r-- | day14/__init__.py | 3 | ||||
| -rw-r--r-- | day15/__init__.py | 15 | ||||
| -rw-r--r-- | day17/__init__.py | 42 | ||||
| -rw-r--r-- | day17/example.txt | 1 | ||||
| -rw-r--r-- | day17/input.txt | 1 | ||||
| -rw-r--r-- | day9/__init__.py | 70 |
8 files changed, 120 insertions, 56 deletions
diff --git a/aoc/datastructures.py b/aoc/datastructures.py new file mode 100644 index 0000000..c49f564 --- /dev/null +++ b/aoc/datastructures.py | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from collections import namedtuple | ||
| 3 | |||
| 4 | |||
| 5 | class Coordinate(namedtuple("Coordinate", ["x", "y"])): | ||
| 6 | def __sub__(self, other: "Coordinate"): | ||
| 7 | return Coordinate(self.x - other.x, self.y - other.y) | ||
| 8 | |||
| 9 | def __add__(self, other: "Coordinate"): | ||
| 10 | return Coordinate(self.x + other.x, self.y + other.y) | ||
| 11 | |||
| 12 | def manhattan_distance(self, other: "Coordinate"): | ||
| 13 | return abs(self.x - other.x) + abs(self.y - other.y) | ||
| 14 | |||
| 15 | @property | ||
| 16 | def polarity_x(self): | ||
| 17 | try: | ||
| 18 | return abs(self.x) / self.x | ||
| 19 | except ZeroDivisionError: | ||
| 20 | return 0 | ||
| 21 | |||
| 22 | @property | ||
| 23 | def polarity_y(self): | ||
| 24 | try: | ||
| 25 | return abs(self.y) / self.y | ||
| 26 | except ZeroDivisionError: | ||
| 27 | return 0 | ||
diff --git a/aoc/decorators.py b/aoc/decorators.py new file mode 100644 index 0000000..a31993a --- /dev/null +++ b/aoc/decorators.py | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from functools import wraps | ||
| 3 | from typing import Iterator, TypeVar, Callable | ||
| 4 | |||
| 5 | T = TypeVar("T") | ||
| 6 | |||
| 7 | |||
| 8 | def infinite_generator(func: Callable[[...], Iterator[T]]): | ||
| 9 | @wraps(func) | ||
| 10 | def wrapper(*args, **kwargs): | ||
| 11 | items = list(func(*args, **kwargs)) | ||
| 12 | |||
| 13 | while True: | ||
| 14 | for item in items: | ||
| 15 | yield item | ||
| 16 | |||
| 17 | return wrapper | ||
diff --git a/day14/__init__.py b/day14/__init__.py index 44107de..8869c79 100644 --- a/day14/__init__.py +++ b/day14/__init__.py | |||
| @@ -4,8 +4,7 @@ from functools import lru_cache | |||
| 4 | from typing import Tuple, Iterator, Set, Any, Optional | 4 | from typing import Tuple, Iterator, Set, Any, Optional |
| 5 | 5 | ||
| 6 | from aoc import BaseAssignment | 6 | from aoc import BaseAssignment |
| 7 | 7 | from aoc.datastructures import Coordinate | |
| 8 | Coordinate = Tuple[int, int] | ||
| 9 | 8 | ||
| 10 | 9 | ||
| 11 | class Assignment(BaseAssignment, ABC): | 10 | class Assignment(BaseAssignment, ABC): |
diff --git a/day15/__init__.py b/day15/__init__.py index 5885f37..38bef35 100644 --- a/day15/__init__.py +++ b/day15/__init__.py | |||
| @@ -1,17 +1,12 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | import re | 2 | import re |
| 3 | from abc import ABC | 3 | from abc import ABC |
| 4 | from collections import namedtuple | ||
| 5 | from dataclasses import dataclass, field | 4 | from dataclasses import dataclass, field |
| 6 | from typing import Tuple, Iterator, Any, Set, Union | 5 | from typing import Union |
| 7 | from typing import Tuple, Iterator, Any, Set, List | 6 | from typing import Tuple, Iterator, Set, List |
| 8 | 7 | ||
| 9 | from aoc import BaseAssignment | 8 | from aoc import BaseAssignment |
| 10 | 9 | from aoc.datastructures import Coordinate | |
| 11 | |||
| 12 | class Coordinate(namedtuple("Coordinate", ["x", "y"])): | ||
| 13 | def distance(self, other: "Coordinate"): | ||
| 14 | return abs(self.x - other.x) + abs(self.y - other.y) | ||
| 15 | 10 | ||
| 16 | 11 | ||
| 17 | @dataclass | 12 | @dataclass |
| @@ -24,10 +19,10 @@ class Sensor: | |||
| 24 | return hash(self.coordinate) | 19 | return hash(self.coordinate) |
| 25 | 20 | ||
| 26 | def __post_init__(self): | 21 | def __post_init__(self): |
| 27 | self.radius = self.coordinate.distance(self.nearest) | 22 | self.radius = self.coordinate.manhattan_distance(self.nearest) |
| 28 | 23 | ||
| 29 | def within_radius(self, coordinate: Coordinate) -> bool: | 24 | def within_radius(self, coordinate: Coordinate) -> bool: |
| 30 | distance = self.coordinate.distance(coordinate) | 25 | distance = self.coordinate.manhattan_distance(coordinate) |
| 31 | return distance <= self.radius | 26 | return distance <= self.radius |
| 32 | 27 | ||
| 33 | def x_coordinates_within_radius_at(self, y: int, map: "Map") -> Union[range, list]: | 28 | def x_coordinates_within_radius_at(self, y: int, map: "Map") -> Union[range, list]: |
diff --git a/day17/__init__.py b/day17/__init__.py new file mode 100644 index 0000000..41b25dc --- /dev/null +++ b/day17/__init__.py | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from dataclasses import dataclass | ||
| 4 | from typing import Iterator | ||
| 5 | |||
| 6 | from aoc import BaseAssignment | ||
| 7 | from aoc.datastructures import Coordinate | ||
| 8 | from aoc.decorators import infinite_generator | ||
| 9 | |||
| 10 | |||
| 11 | @dataclass | ||
| 12 | class Block: | ||
| 13 | origin: Coordinate | ||
| 14 | |||
| 15 | |||
| 16 | class Assignment(BaseAssignment[int, str], ABC): | ||
| 17 | @infinite_generator | ||
| 18 | def infinite_moves(self, item: str) -> str: | ||
| 19 | return item | ||
| 20 | |||
| 21 | def infinite_blocks(self): | ||
| 22 | pass | ||
| 23 | |||
| 24 | |||
| 25 | class AssignmentOne(Assignment): | ||
| 26 | example_result = 1 | ||
| 27 | |||
| 28 | def run(self, input: Iterator[str]): | ||
| 29 | i = 0 | ||
| 30 | |||
| 31 | moves = self.infinite_moves(input) | ||
| 32 | |||
| 33 | for move in self.infinite_moves(next(input)): | ||
| 34 | i += 1 | ||
| 35 | print(move) | ||
| 36 | |||
| 37 | if i == 100: | ||
| 38 | break | ||
| 39 | |||
| 40 | |||
| 41 | class AssignmentTwo(Assignment): | ||
| 42 | pass | ||
diff --git a/day17/example.txt b/day17/example.txt new file mode 100644 index 0000000..97a1aa1 --- /dev/null +++ b/day17/example.txt | |||
| @@ -0,0 +1 @@ | |||
| >>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> | |||
diff --git a/day17/input.txt b/day17/input.txt new file mode 100644 index 0000000..3edbacf --- /dev/null +++ b/day17/input.txt | |||
| @@ -0,0 +1 @@ | |||
| >><<<<><><<>><>><<><<<>><>><<>><>><<>>><<<<>>>><<<<><>><><<>>>><><<<>>>><>><<><<<<>>><<<<>>>><<<>>><>>><>>>><<>>><<<>>><<<>><<>><<<>>><>>>><<<>><<<>>>><>>><><<>><<<><>>>><<<>>>><<>><<>>>><>><<<><<><<<<><<><<<>>>><<<>><<<<>><<<>><<<>><<<<>>>><<<>>>><<<>>>><<>>>><<<>><<<<><<<><>>>><<>>><<<<>><><>>>><<<<><<<<>>><<<>>><<<>><<<<>>>><<<>>>><<<<>>>><<<><<<>><<<>>><<<>>>><><<>><<><<<><>>><<<<>><<<>>><<>>>><><>>><>>>><>><>>>><<<<><<>>><<<<>><<<><<<<>>><<>>>><>>><<<>>><>><<>>>><<<><>>>><<>>><>><><<<<><<<>><<<<>><<<<><>>>><<><<>>><<>><>><>>><<><>>><<<<>>><>>>><<<>><>>>><<<<>>>><>>><>><>>><<>>><<>>><<<<>>>><<<<><<<<>><<>>><><<<><>><>>><<<>>><<<>><<>>><<<><<>>>><<<>>><<<>><><<>>><<<>><<<>><<<<>><><<>><<<<><<>>>><<<>>>><<<<><>>>><><<<>>>><<<<>>><>><<>><<<<><<<<>>>><<<>>><<<>>><><<>>><<>>>><<<>>><<>>>><<<>>><<<<><<<><<><<>><>>>><><<<>>>><<<<>>><<<>><<<<>>>><<>><<>>><>>><<>><>><<<>>><>>>><<>>>><<<>>>><>>>><<<>><<<<><<<<>>><>>>><><>>>><<<<><<<<><>><<<>><<<>>><<<<><<<><<<>>>><<><<>>>><>>>><<<>>>><>>><<<<>>><><<>>><<<>>><<>><>><>>>><<><<<<><<>>><><<<<>>>><><>><<>><<<><<<<>><<<<>><<<>>>><><>><<>>><<>>><<<<>>><<>>><<>><>>>><<<<>>><<<<><<<<>>>><<<>>><<>>>><<<<><>>><<<<><<<<>>><>>><<<<><>><<<<>>><<<<>>><<<<>>><<<>>><>>>><<<<>>><><<<<><>>>><<<<>><>><<<>>>><<<><>><<>><>>>><<><<<>>>><>><<>>>><<<>>><<<>>>><<>>><<>><<<>>>><<<><<<><<<>><<<>>><>>><<<<>>><<>><>>><<<<>>><>><<<>>><>><<<<>>>><><<<><<>>><>>><>>>><<>><<<<>>><<<><<<<>>>><><>>>><<<<>>><<<<><<><<<><<<<><<<<>>><>>><<>>><<<<><<><<<><<<<><<<<><>>><<<>>>><<<>>><>><>><<<<>>>><<<<>>>><>>><<>>><<>>>><<<<>>>><<<<>><<<<>><<><<<<>>>><<>>>><<>><<<<><>>>><<<<>>><<>>><<>>><>><>>><>>><<<<>>><<<<>><<<<>><<<<>><<<>><<>><>><>>><<<><<><<>>>><>>><<<>>><<<>>><<>>>><><<<<>>>><<>>><<<<>>><><<>>>><<>>><<>>>><<>>>><<><>>><<<<>>>><<>>>><<<<><<<>>><<<>>>><<<<>>><<<>><<<<>>><<<>>><<<>><<<<><<<<><>>><>>><<<<><<<<><<<<><<<<>>><<<<>><<>><>><>>><<<<>>>><>>>><<<>><>>><>>>><<<>><<>>>><<<<>><<<><<>><<<>><><><<<><<<>>><>>>><<>><>>><>>>><>>>><<<<>>>><<<<>>>><<<<>>><<>><<>>><<>><<><>><<<>>>><>>><>>><<<>><><<<<>>><<<><<<>>>><<<<>>><<<>>>><<<><<<<>>>><<>>><<<<><>>><<><>>>><<<<><<><<<>><<<<>>>><<>>><<>><<<>><<<>>>><>><>>><<<>><<<<>>>><<<<>>>><>>>><<>><<<<><<>><<>>><<<>><<<<><<<>><<<<>>>><<<<>><<>><<<>>><<>><><>>>><<<>><<<>>><<<<>>><<<<>>><<<<><<<><<<><<<<>>>><<>><<<<>>>><<>>>><<<>>><<><>>><>>>><<<<><<<><<>>>><<<>>>><<<<>><><<<>>><<<<>>><<>><<<>>>><<<>>><<<<><<>>>><<<>>>><<<<><<<<>>>><<<<>><>><<<>>>><>><<>>>><><<<><<<<>><<>><<<<>>>><<>><>>><<>>>><<<>>>><<<<>><<<>><<<<>><>><<>>>><>>><>>><<>>>><<<<>>>><<>>>><<<>><<<<>>>><<<>>><>>>><<<<>>>><<>>>><<<<>><<<><<><<<<>>>><<>>><<><>>>><>><>><>><<<><<<<>><>>>><<<><>>>><<>><<<<><<>>>><<><<<>><<<><<<>>>><<<<><>>>><<<>>>><<<>>><><>><>>><<<>>><<>>><<><<<>>>><>><<<<>>>><>>>><<>>>><<<>>><<<>>>><><><<<<>><<<<>>><><<>><<>>><><<<<><>>><>>>><<<>>><<<<><<<>><<<<>><<<>><<<<><<<><<<<>><<<<>><>>>><<<<>>><>>><>><>>>><<<>><<<>>><<<<>><<<>>>><<<<>><<<>>>><<<>>>><<<>><>><<<>>>><>><<<<>>>><<<<><<>>>><<<><<<>>>><<><<><<>>><<<<><>>>><><<<<>>>><<>>>><<<>>><<><<<<><<>><<>><<<<>>>><<<<>>><>>><<>>><<><<>><<<<><<>>><<<<>><<<>><<<>><><<>>>><<<><<><<<><<<<>>><>>><><<>><<<<>>><>>>><<<<>>><>><<<<>><<<>><<<>>><<<><<>>><<<><<<<>><<<<>>><<<<>>><<<>>><<<><<<<><>>>><<<<>><><<<<>>><<<<>>>><<<><<<<><<><>><<<>><<>>>><><<>>><<<>>>><>>><<<><<<<><<<>><<<<>>><<>><<<>>><<<>>><><<<<><>><<>>>><<>><<<>>><>>>><<<>><<<>>>><<><<<<>><><<<<>>>><<<<>>><<<>><<>><<<>>><<<<>>><<<>>>><>>><<<<><<<>><<><<<>>><<<<>>>><><<>>>><>>>><<<>>>><>>>><<>><<>><>>>><<<>>><<<>>><<<>><>><>>><<<<><>>>><<<><<<>>>><<>>><<<<>>><<<>>><>>>><<<<>>><<>>><<>>>><<<<>>>><>>>><<<>>>><><><<><<>>>><<<>>>><>>>><<<>>><<<<><>>>><>><>><>>><<<<><<><><<<><<>>>><<><<>>><<<<>>>><<<<>>>><<<>><>>>><<<>><<>><<>><>>>><<<>>><<<<><>><<><<<<>>>><<<<>><<<>><><<<<><<<>>><<>>>><<<<>><>>><<<>><<>>><>>><<>>><>>>><<><<<<><>>>><<<>><<<<>>>><<>>><>>>><>>>><<>>><<>><><<>>><<>>><<<>><<<<>><<<>>><>>><<<><>>><<<>>>><<><<<<>><<<<>>>><>>>><<<<>><<>>>><<<>>><<><>><>><<<>>><<<<>><<<<><<>>>><<>><<<>>>><<<<><<<<>>>><<<<>>><<<>>><<>>>><<<<>>><<>><<>><>>>><<>>>><<<><<>>>><<><<>>>><<>>><<>>>><><<<><<<>><<>><<<<>><>>>><<>>><<>><<<<><<><<<><>>>><<<<>>>><<<>>><<<>>>><<<<><<<><<>><<>><<<>>>><>>><<>>><><<>><<<><<<>>><<<><<<>>>><>><<>><<<<>>>><><>>><<<<>>>><>>>><<<>>><>><<<>>><<>>>><<<<>><<<<>>><<<><>>>><>>>><>><<<<>>><><<<<>>><<<<>>>><>><<<>>><<<><>>>><>>><<<>>>><<>><<>><<<>>>><<><><>><<>>><<<><<<><><<<<><<>>>><<<>><<<><<<<>>>><<>><<<<><<<>>>><<<<>>><<<<>>>><<<>><<><>>><<>><<<><<<>><<>>>><<<>>>><>>><<>>><<<>>>><<>>><<<>><<><<>><><<>>><<>>>><<<<>><><<>>>><<>>>><>>>><>><>><<<<>>><>>><<<><>>><<><<><<<><>>><<<><<>>>><<<<>>><<<<>>><<<>><<>>>><<<<>>>><><<<><>>><>>><<>>><<<<>><>><<<>>><<<>><><><<<<>>><>>>><><<>><<<>>>><>>><<<<><<<>><>>>><<><<<<>>><<<<>>>><><<<><<<>>>><><<<>>><<<<>>>><>>>><<<<><><<<<>>>><<>>><>>><><<>>>><<<<>>>><<<<>>>><<<<>><>>>><<<>>><<>>><<<<>><<<><>><<>>><>>><<<>>><<<<>>><<><<<>><<<>><<<<>><<<>><>>><<><<<<>><<>>><<<<><<>>><>>>><>><<<<>>><>>>><><<>>><><>>><<<><>>>><<<>>><<><><<<><<<<>>><<>><<<<><<><<>>><<>>>><<<<>><>>>><<><<><>><<<><>><<<>>>><<<>>><<<>>>><<><<<>><<<<><<<>>><<<<>>>><<>><>>><<<><<<<><<<>>><><<>>><<<<><<<<>>>><<><><<<<>>><<<<>>>><<<>>><<<>>><<<>>><<<>>><<<<>>>><<<<>><<>>>><<>>><>>><<<>>><<>>><<<<>>>><<>><<<>>><><<<<>>><>>><<<<><<<>>><>>>><>><<>><>>>><>>>><<<<>>><>><<<><<<><<>>>><<>>>><<>>><<<<>>>><>>>><<<><<<><<<<>>><<>>><<>><<<<>>><<<>>>><>>>><<>>>><<<><><<<<>>>><>>>><>>><>>><>>>><<<><<<<><<><><<<>>>><<<>>><<><<>>><<<><<<>><<<<>>>><<<<>>><<<><<<<>><<<>><><<>>>><>>><>>><><<>>>><<<<>><<<><<>>>><>>><<<>>>><>>><<<>><>>>><<>><>>><<>>>><<<<>>>><>>>><>>>><>>><<>><>>>><>><><<<>>>><><<<><<>>>><<<<>><>><<<<><<<<><>>>><<>>><<<<>><>><<<>><<<><<<><<<><<<<>>><<>>><><<<<>><>><<<>><<>>>><<<>>><<<>>><<<<>>><<>><<>>>><<>><<<<>>><>><><<>>>><>>><>><<<<><<<<>>>><<<><>>><>>><<<>>><>>>><<<><<>>><><>>>><<<>>><>>>><><<>>><<>>>><<>>><<>>><<<>>><<<>>><><<<<>>><<<<>><<>>>><<<<>>><<<<>>><<>>>><<<>>><<<<>>>><<<<>><<><<><<>>><>>><>><<><<<>>>><<>>><<<<>><<<<>>>><<<<>><>><<<<>><<<<>>><<<>>><<>>><<>><<<<>><<<>><<<><<<<>>><>><<<>>>><<>><<<<><<<<>>><<<>>>><>>>><<<>><>>><<<<><<>>><>><<<<>>><>>><<<><<<>><<>><>>>><<>><<<>>><>><<>>>><<<>>>><<<<><>>>><<<<>>><<<>><<<<>>><<<<>>>><<><<>>>><>><<>>>><<<>><><><><<<<>><<<<><<<><<>>>><>>><<<<>>><<>>>><<<>>>><<<<><<<<>><<>>>><>><<>>><><>><>>><<>><>><<>>><>><>>><>>>><>><>>><<><><<<><<<<>><<<<><>>>><>><<<>><<<>>>><<>>>><<>><<<<>>><>>>><<<><<<>>>><<<<><<<<>><><<><<><>><><<<<>>>><<<>>><<<><><<<<>>>><<<<>><<<<><>><<<<>>><<<<><<<>>><>>><<><<<<>>>><<<<>>>><<<<><<<<><<<>>>><<<>>>><>>><<>><<<><<<>>>><<>>>><<<<>><<<<>><<<>>><<><<<>>><><<<<>>><<<>>>><<>>>><>>><<><<>>>><<<<>><<<>><<<>><>><<>>>><<<>>>><<><<<><>>>><>>><>>><<<<>>>><<>>><<<<>><>>><>>><<<>>>><<>>>><>><<>>><<<<>>><<><<>>>><<<<>>>><<>>><<><<><<>>>><<<>>><<<>>>><<>>>><>><<<>>><<<<>>><<<><<<><<<<>><><>>>><<<<><>>><<<>>><>>>><<>>><<<>>>><<<<>>><<><<<<>>><<<<>>><<>>><>>>><<<<>>>><<<<>>>><<<><<<<>><<<><<>>><>><<>>>><<<<>>>><<<<>><>><<>><<<<>>><<<<><>>><<>><<<<>><<><>>><<<<>><<<>>><><<>>><<>><<>>><<<>>><<<<>>><<<<>><>>>><<<>>>><<>><<<><<<>>>><>>>><>>><<>><<>>>><><><<<<>><<<>>>><<><<<<><<<>>>><<<<><>>><<>><<<<>><><<<<>>><<>>><<<<><<<<>>>><<><>>><<<>>>><<<>>>><>>><>>>><<<>>>><>><>>><<<<>><<>>>><<<<>>>><<<>><<>>>><>><<<<>>>><<<<>><<<<>><<<<><<<<>><<<>>><<<>>>><>>>><<>><<<><<>>>><<<>>>><<>>><<<>>><<<>><>>>><>>><<<<>>>><<>>><>>>><<<>>>><<>>><<>><<><>>>><>><<<><<>><>>><<<>>>><>><>>>><><<>><<<>><<<<>>><<<>><><<<<>>>><<>>>><<<>>>><<>><<<<>><<<>>>><>><>>><>><<>>>><<<><<<<>>><<>><<<>>><<<<>>><<>><<>><>>><<<>><>><<><<>>><<<<>>>><<<>>>><<><><>><<<<>>><<<<>>>><><<>>>><>>>><<<>><<<><<>>><<><>>><<>><<>>>><>>><<>>>><<<><<><<<<><<<<>>><<>>><>>><><<>><<<<>>><<<>>>><>>><<><<<<>>>><<<>>><<><<<<>>>><<<<>>><<>>>><<<>>><<<>><<<>>>><<<><>>>><<<<>><<<<><>><<<<>>><<><<<<>>><>><>>>><<<>>><<<>>><><<<>><<<>>><<<>><<>>><<<><<<>>><<<<>><<<>>><<<<>>>><>>>><<>><<>>><<<<>>><<<<>>>><<>><<<><<><<<<>>>><<<>>>><>><<>>><<<>><<<<>>>><<>><>>><<<<>>><>><><<<>><<<<>>>><<<>>><<>>><<<<>>><>><>>>><<>>>><>>><>>><>>>><><<<<>>><>>>><<>><<<<>>>><>>>><<<<>><>>><>>>><<<<>><><>><><<<<>>>><<<<>>><<<<>><<<<><<<>>><<>>>><<<>>>><><><<<<>><<<<>>>><<>>>><<<<>>>><<<>>><<<><<<>>>><<<>>><<>>>><<><>><<<<>>>><<>>>><<<<>>><>>><><<<<>>>><>>><<>>>><<>>><>>>><<<<>>>><<>><<>><<<<>><<<>>>><<>><<<<><<><<<>><<>>>><>>>><><>><<><<<><<<<>><>>>><>>><>>>><>>><<<<>><<<<>><>><<<<>><<>><<<<>><<<<>><>>><<<<><<<>>>><<<<><<>><<<<>>><<>>><<<>>><>>><<>>>><<<><<<<><>>><<<>>>><>>><>>><><<<<><<<<>><<<<>><<<<>><<<><<<><<<>>><<<>><<>>><<>><>><<>>><>>>><<<<>>>><>><>><<<<><<<<>><<>>><>><<>><<>>>><<<>><<>><<<>><><>>>><><<>><<<>>>><<<<><>>>><<>>>><><>><><<>><<>>><<<><<<<><>><<>>><<<>>>><<><<>>><<<<>><<<<>><<<>>><<>>><<<<><>>><>><<<<><<<<>>>><<<>><>><<><<<<>><>>><<>>>><<<<>>>><<<<><>>><<>>><>>><><>>>><<<<>><<<<>>><<><<<>>><<<<>>>><>><<<>>><<>><<>>>><<<<><>>>><<>>><<<<><<<>>><<<>><<<<>>><<<<>><<><>>><<<<>><<<>><<<<><<<<>>><<<<>>>><<<>><<><<<>>>><>>><<>><<<>>><<<<><>><>><<>>><<<><><>><>><>><<>>><<>>>><<<>><>>><<><<<<>><>><<<<>>><<><><<>>><<<<>>>><<>><<<>>><>>>><<<<>>>><<<>><<<<><<<><<>><>><<><>>><<<<>>>><<<><<<<>><<<>><<>>><><<><<<>>>><<<<><<<<>>><>>>><><<<<>>>><<<><<>>><<>><<><>>>><<<>>><<<<><<<>><<><<<>>><<>><><<<><<>><><<<>><>><<<<>>><<<>><>>><<>>>><><<<>><<<<>>><<<<>>><<>><>>>><<<<>>><<<<>>>><><<>>>><>>>><<>>><<<>><<<>><<<>>><<<>>><<<>><>>><<<<>><<<>>>><<>>><><<<<>><><>><>><<<<>><<<<>>><<>>><<<<>>><>>>><>><<<<>>><<>>>><<<<>>><>><>>>><>>><>><<>>>><<>>>><<<>><<>><<<<><<>><<<>><<<>>>><<<<>>><<<<><<<><>><<<>><<><<>><<>>>><<>><<<<>>><<<<><><<<>><>>>><<><<>><<<<>>><<<<>>>><<<<><<<<>>>><<<>>><<>>><<<><<<<>>>><>><<<<>>>><<<><>>><<<>>>><<>>><<<>>><<<>><<<<>>><<>>>><>>><>><<<<>>><><<>><<>><<<<><<<>>><<>><>><<>>><><<><<>><<>>><>><<<<>>><><<>>>><>><<<<>>>><>><>>>><<<<>>>><<<>>>><>>><><<<><><>><<<<>><<<><>><<<<>>><<><<>><<<>>><<<<>>><<<<>>><><<<>>><<>><<<<><<<><<<>>><>>>><<>>><<<<><<>><<<><<<><<>>>><<<>><<<<><>>><<<<>>>><>><><<>><<>>>><<<><<>><>>><>><<>>>><>>>><<>>><<<>><<<>>><>>>><>><<<><<<<><<<<>>>><<<<><<>>>><<>><><<<<>>><<<<><>>>><>>><<>>>><><<<>>>><<<<>><<>>><<>><<><<>><>>><<<<>><<>>>><<<>>>><<><<<>>>><<<>>><<><<>><>><><<>< | |||
diff --git a/day9/__init__.py b/day9/__init__.py index e0f1d61..a2d6d82 100644 --- a/day9/__init__.py +++ b/day9/__init__.py | |||
| @@ -1,36 +1,10 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | from abc import ABC | 2 | from abc import ABC |
| 3 | from copy import copy | 3 | from copy import copy |
| 4 | from dataclasses import dataclass | ||
| 5 | from typing import Tuple, Iterator, List, Set | 4 | from typing import Tuple, Iterator, List, Set |
| 6 | 5 | ||
| 7 | from aoc import BaseAssignment | 6 | from aoc import BaseAssignment |
| 8 | 7 | from aoc.datastructures import Coordinate | |
| 9 | |||
| 10 | @dataclass | ||
| 11 | class Coordinate: | ||
| 12 | x: int | ||
| 13 | y: int | ||
| 14 | |||
| 15 | def __hash__(self): | ||
| 16 | return tuple([self.x, self.y]).__hash__() | ||
| 17 | |||
| 18 | def __sub__(self, other: "Coordinate"): | ||
| 19 | return Coordinate(self.x - other.x, self.y - other.y) | ||
| 20 | |||
| 21 | @property | ||
| 22 | def polarity_x(self): | ||
| 23 | try: | ||
| 24 | return abs(self.x) / self.x | ||
| 25 | except ZeroDivisionError: | ||
| 26 | return 0 | ||
| 27 | |||
| 28 | @property | ||
| 29 | def polarity_y(self): | ||
| 30 | try: | ||
| 31 | return abs(self.y) / self.y | ||
| 32 | except ZeroDivisionError: | ||
| 33 | return 0 | ||
| 34 | 8 | ||
| 35 | 9 | ||
| 36 | class Assignment(BaseAssignment, ABC): | 10 | class Assignment(BaseAssignment, ABC): |
| @@ -44,36 +18,44 @@ class Assignment(BaseAssignment, ABC): | |||
| 44 | pass | 18 | pass |
| 45 | 19 | ||
| 46 | @staticmethod | 20 | @staticmethod |
| 47 | def next_head(head: Coordinate, direction: str): | 21 | def next_head(head: Coordinate, direction: str) -> Coordinate: |
| 48 | match direction: | 22 | match direction: |
| 49 | case "R": | 23 | case "R": |
| 50 | head.x += 1 | 24 | return head + Coordinate(1, 0) |
| 51 | case "L": | 25 | case "L": |
| 52 | head.x -= 1 | 26 | return head + Coordinate(-1, 0) |
| 53 | case "U": | 27 | case "U": |
| 54 | head.y += 1 | 28 | return head + Coordinate(0, 1) |
| 55 | case "D": | 29 | case "D": |
| 56 | head.y -= 1 | 30 | return head + Coordinate(0, -1) |
| 57 | 31 | ||
| 58 | @staticmethod | 32 | @staticmethod |
| 59 | def next_knot(head: Coordinate, tail: Coordinate): | 33 | def next_knot(head: Coordinate, tail: Coordinate) -> Coordinate: |
| 60 | delta = head - tail | 34 | delta = head - tail |
| 61 | 35 | ||
| 62 | if abs(delta.x) < 2 and abs(delta.y) < 2: | 36 | if abs(delta.x) < 2 and abs(delta.y) < 2: |
| 63 | pass | 37 | return tail |
| 64 | elif abs(delta.x) > 1 and delta.y == 0: | 38 | elif abs(delta.x) > 1 and delta.y == 0: |
| 65 | tail.x += delta.x - delta.polarity_x | 39 | return tail + Coordinate( |
| 40 | delta.x - delta.polarity_x, | ||
| 41 | 0, | ||
| 42 | ) | ||
| 66 | elif abs(delta.x) > 1 and abs(delta.y) == 1: | 43 | elif abs(delta.x) > 1 and abs(delta.y) == 1: |
| 67 | tail.x += delta.x - delta.polarity_x | 44 | return tail + Coordinate(delta.x - delta.polarity_x, delta.y) |
| 68 | tail.y += delta.y | ||
| 69 | elif abs(delta.y) > 1 and delta.x == 0: | 45 | elif abs(delta.y) > 1 and delta.x == 0: |
| 70 | tail.y += delta.y - delta.polarity_y | 46 | return tail + Coordinate( |
| 47 | 0, | ||
| 48 | delta.y - delta.polarity_y, | ||
| 49 | ) | ||
| 71 | elif abs(delta.y) > 1 and abs(delta.x) == 1: | 50 | elif abs(delta.y) > 1 and abs(delta.x) == 1: |
| 72 | tail.y += delta.y - delta.polarity_y | 51 | return tail + Coordinate( |
| 73 | tail.x += delta.x | 52 | delta.x, |
| 53 | delta.y - delta.polarity_y, | ||
| 54 | ) | ||
| 74 | elif abs(delta.x) > 1 and abs(delta.y) > 1: | 55 | elif abs(delta.x) > 1 and abs(delta.y) > 1: |
| 75 | tail.x += delta.x - delta.polarity_x | 56 | return tail + Coordinate( |
| 76 | tail.y += delta.y - delta.polarity_y | 57 | delta.x - delta.polarity_x, delta.y - delta.polarity_y |
| 58 | ) | ||
| 77 | 59 | ||
| 78 | def tail_positions(self, input: Iterator[str], length: int) -> Iterator[Coordinate]: | 60 | def tail_positions(self, input: Iterator[str], length: int) -> Iterator[Coordinate]: |
| 79 | knots = [Coordinate(0, 0) for _ in range(length)] | 61 | knots = [Coordinate(0, 0) for _ in range(length)] |
| @@ -84,10 +66,10 @@ class Assignment(BaseAssignment, ABC): | |||
| 84 | for _ in range(amount): | 66 | for _ in range(amount): |
| 85 | for index in range(length): | 67 | for index in range(length): |
| 86 | if index == 0: | 68 | if index == 0: |
| 87 | self.next_head(knots[index], direction) | 69 | knots[0] = self.next_head(knots[0], direction) |
| 88 | continue | 70 | continue |
| 89 | 71 | ||
| 90 | self.next_knot(knots[index - 1], knots[index]) | 72 | knots[index] = self.next_knot(knots[index - 1], knots[index]) |
| 91 | 73 | ||
| 92 | yield knots[length - 1] | 74 | yield knots[length - 1] |
| 93 | 75 | ||
