diff options
| -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 @@ | |||
| >><<<<><><<>><>><<><<<>><>><<>><>><<>>><<<<>>>><<<<><>><><<>>>><><<<>>>><>><<><<<<>>><<<<>>>><<<>>><>>><>>>><<>>><<<>>><<<>><<>><<<>>><>>>><<<>><<<>>>><>>><><<>><<<><>>>><<<>>>><<>><<>>>><>><<<><<><<<<><<><<<>>>><<<>><<<<>><<<>><<<>><<<<>>>><<<>>>><<<>>>><<>>>><<<>><<<<><<<><>>>><<>>><<<<>><><>>>><<<<><<<<>>><<<>>><<<>><<<<>>>><<<>>>><<<<>>>><<<><<<>><<<>>><<<>>>><><<>><<><<<><>>><<<<>><<<>>><<>>>><><>>><>>>><>><>>>><<<<><<>>><<<<>><<<><<<<>>><<>>>><>>><<<>>><>><<>>>><<<><>>>><<>>><>><><<<<><<<>><<<<>><<<<><>>>><<><<>>><<>><>><>>><<><>>><<<<>>><>>>><<<>><>>>><<<<>>>><>>><>><>>><<>>><<>>><<<<>>>><<<<><<<<>><<>>><><<<><>><>>><<<>>><<<>><<>>><<<><<>>>><<<>>><<<>><><<>>><<<>><<<>><<<<>><><<>><<<<><<>>>><<<>>>><<<<><>>>><><<<>>>><<<<>>><>><<>><<<<><<<<>>>><<<>>><<<>>><><<>>><<>>>><<<>>><<>>>><<<>>><<<<><<<><<><<>><>>>><><<<>>>><<<<>>><<<>><<<<>>>><<>><<>>><>>><<>><>><<<>>><>>>><<>>>><<<>>>><>>>><<<>><<<<><<<<>>><>>>><><>>>><<<<><<<<><>><<<>><<<>>><<<<><<<><<<>>>><<><<>>>><>>>><<<>>>><>>><<<<>>><><<>>><<<>>><<>><>><>>>><<><<<<><<>>><><<<<>>>><><>><<>><<<><<<<>><<<<>><<<>>>><><>><<>>><<>>><<<<>>><<>>><<>><>>>><<<<>>><<<<><<<<>>>><<<>>><<>>>><<<<><>>><<<<><<<<>>><>>><<<<><>><<<<>>><<<<>>><<<<>>><<<>>><>>>><<<<>>><><<<<><>>>><<<<>><>><<<>>>><<<><>><<>><>>>><<><<<>>>><>><<>>>><<<>>><<<>>>><<>>><<>><<<>>>><<<><<<><<<>><<<>>><>>><<<<>>><<>><>>><<<<>>><>><<<>>><>><<<<>>>><><<<><<>>><>>><>>>><<>><<<<>>><<<><<<<>>>><><>>>><<<<>>><<<<><<><<<><<<<><<<<>>><>>><<>>><<<<><<><<<><<<<><<<<><>>><<<>>>><<<>>><>><>><<<<>>>><<<<>>>><>>><<>>><<>>>><<<<>>>><<<<>><<<<>><<><<<<>>>><<>>>><<>><<<<><>>>><<<<>>><<>>><<>>><>><>>><>>><<<<>>><<<<>><<<<>><<<<>><<<>><<>><>><>>><<<><<><<>>>><>>><<<>>><<<>>><<>>>><><<<<>>>><<>>><<<<>>><><<>>>><<>>><<>>>><<>>>><<><>>><<<<>>>><<>>>><<<<><<<>>><<<>>>><<<<>>><<<>><<<<>>><<<>>><<<>><<<<><<<<><>>><>>><<<<><<<<><<<<><<<<>>><<<<>><<>><>><>>><<<<>>>><>>>><<<>><>>><>>>><<<>><<>>>><<<<>><<<><<>><<<>><><><<<><<<>>><>>>><<>><>>><>>>><>>>><<<<>>>><<<<>>>><<<<>>><<>><<>>><<>><<><>><<<>>>><>>><>>><<<>><><<<<>>><<<><<<>>>><<<<>>><<<>>>><<<><<<<>>>><<>>><<<<><>>><<><>>>><<<<><<><<<>><<<<>>>><<>>><<>><<<>><<<>>>><>><>>><<<>><<<<>>>><<<<>>>><>>>><<>><<<<><<>><<>>><<<>><<<<><<<>><<<<>>>><<<<>><<>><<<>>><<>><><>>>><<<>><<<>>><<<<>>><<<<>>><<<<><<<><<<><<<<>>>><<>><<<<>>>><<>>>><<<>>><<><>>><>>>><<<<><<<><<>>>><<<>>>><<<<>><><<<>>><<<<>>><<>><<<>>>><<<>>><<<<><<>>>><<<>>>><<<<><<<<>>>><<<<>><>><<<>>>><>><<>>>><><<<><<<<>><<>><<<<>>>><<>><>>><<>>>><<<>>>><<<<>><<<>><<<<>><>><<>>>><>>><>>><<>>>><<<<>>>><<>>>><<<>><<<<>>>><<<>>><>>>><<<<>>>><<>>>><<<<>><<<><<><<<<>>>><<>>><<><>>>><>><>><>><<<><<<<>><>>>><<<><>>>><<>><<<<><<>>>><<><<<>><<<><<<>>>><<<<><>>>><<<>>>><<<>>><><>><>>><<<>>><<>>><<><<<>>>><>><<<<>>>><>>>><<>>>><<<>>><<<>>>><><><<<<>><<<<>>><><<>><<>>><><<<<><>>><>>>><<<>>><<<<><<<>><<<<>><<<>><<<<><<<><<<<>><<<<>><>>>><<<<>>><>>><>><>>>><<<>><<<>>><<<<>><<<>>>><<<<>><<<>>>><<<>>>><<<>><>><<<>>>><>><<<<>>>><<<<><<>>>><<<><<<>>>><<><<><<>>><<<<><>>>><><<<<>>>><<>>>><<<>>><<><<<<><<>><<>><<<<>>>><<<<>>><>>><<>>><<><<>><<<<><<>>><<<<>><<<>><<<>><><<>>>><<<><<><<<><<<<>>><>>><><<>><<<<>>><>>>><<<<>>><>><<<<>><<<>><<<>>><<<><<>>><<<><<<<>><<<<>>><<<<>>><<<>>><<<><<<<><>>>><<<<>><><<<<>>><<<<>>>><<<><<<<><<><>><<<>><<>>>><><<>>><<<>>>><>>><<<><<<<><<<>><<<<>>><<>><<<>>><<<>>><><<<<><>><<>>>><<>><<<>>><>>>><<<>><<<>>>><<><<<<>><><<<<>>>><<<<>>><<<>><<>><<<>>><<<<>>><<<>>>><>>><<<<><<<>><<><<<>>><<<<>>>><><<>>>><>>>><<<>>>><>>>><<>><<>><>>>><<<>>><<<>>><<<>><>><>>><<<<><>>>><<<><<<>>>><<>>><<<<>>><<<>>><>>>><<<<>>><<>>><<>>>><<<<>>>><>>>><<<>>>><><><<><<>>>><<<>>>><>>>><<<>>><<<<><>>>><>><>><>>><<<<><<><><<<><<>>>><<><<>>><<<<>>>><<<<>>>><<<>><>>>><<<>><<>><<>><>>>><<<>>><<<<><>><<><<<<>>>><<<<>><<<>><><<<<><<<>>><<>>>><<<<>><>>><<<>><<>>><>>><<>>><>>>><<><<<<><>>>><<<>><<<<>>>><<>>><>>>><>>>><<>>><<>><><<>>><<>>><<<>><<<<>><<<>>><>>><<<><>>><<<>>>><<><<<<>><<<<>>>><>>>><<<<>><<>>>><<<>>><<><>><>><<<>>><<<<>><<<<><<>>>><<>><<<>>>><<<<><<<<>>>><<<<>>><<<>>><<>>>><<<<>>><<>><<>><>>>><<>>>><<<><<>>>><<><<>>>><<>>><<>>>><><<<><<<>><<>><<<<>><>>>><<>>><<>><<<<><<><<<><>>>><<<<>>>><<<>>><<<>>>><<<<><<<><<>><<>><<<>>>><>>><<>>><><<>><<<><<<>>><<<><<<>>>><>><<>><<<<>>>><><>>><<<<>>>><>>>><<<>>><>><<<>>><<>>>><<<<>><<<<>>><<<><>>>><>>>><>><<<<>>><><<<<>>><<<<>>>><>><<<>>><<<><>>>><>>><<<>>>><<>><<>><<<>>>><<><><>><<>>><<<><<<><><<<<><<>>>><<<>><<<><<<<>>>><<>><<<<><<<>>>><<<<>>><<<<>>>><<<>><<><>>><<>><<<><<<>><<>>>><<<>>>><>>><<>>><<<>>>><<>>><<<>><<><<>><><<>>><<>>>><<<<>><><<>>>><<>>>><>>>><>><>><<<<>>><>>><<<><>>><<><<><<<><>>><<<><<>>>><<<<>>><<<<>>><<<>><<>>>><<<<>>>><><<<><>>><>>><<>>><<<<>><>><<<>>><<<>><><><<<<>>><>>>><><<>><<<>>>><>>><<<<><<<>><>>>><<><<<<>>><<<<>>>><><<<><<<>>>><><<<>>><<<<>>>><>>>><<<<><><<<<>>>><<>>><>>><><<>>>><<<<>>>><<<<>>>><<<<>><>>>><<<>>><<>>><<<<>><<<><>><<>>><>>><<<>>><<<<>>><<><<<>><<<>><<<<>><<<>><>>><<><<<<>><<>>><<<<><<>>><>>>><>><<<<>>><>>>><><<>>><><>>><<<><>>>><<<>>><<><><<<><<<<>>><<>><<<<><<><<>>><<>>>><<<<>><>>>><<><<><>><<<><>><<<>>>><<<>>><<<>>>><<><<<>><<<<><<<>>><<<<>>>><<>><>>><<<> | |||
