From 5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sat, 17 Dec 2022 17:17:54 +0100 Subject: Added some utilities --- aoc/datastructures.py | 27 ++++++++++++++++++++ aoc/decorators.py | 17 +++++++++++++ day14/__init__.py | 3 +-- day15/__init__.py | 15 ++++------- day17/__init__.py | 42 +++++++++++++++++++++++++++++++ day17/example.txt | 1 + day17/input.txt | 1 + day9/__init__.py | 70 +++++++++++++++++++-------------------------------- 8 files changed, 120 insertions(+), 56 deletions(-) create mode 100644 aoc/datastructures.py create mode 100644 aoc/decorators.py create mode 100644 day17/__init__.py create mode 100644 day17/example.txt create mode 100644 day17/input.txt 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 @@ +# -*- coding: utf-8 -*- +from collections import namedtuple + + +class Coordinate(namedtuple("Coordinate", ["x", "y"])): + def __sub__(self, other: "Coordinate"): + return Coordinate(self.x - other.x, self.y - other.y) + + def __add__(self, other: "Coordinate"): + return Coordinate(self.x + other.x, self.y + other.y) + + def manhattan_distance(self, other: "Coordinate"): + return abs(self.x - other.x) + abs(self.y - other.y) + + @property + def polarity_x(self): + try: + return abs(self.x) / self.x + except ZeroDivisionError: + return 0 + + @property + def polarity_y(self): + try: + return abs(self.y) / self.y + except ZeroDivisionError: + 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 @@ +# -*- coding: utf-8 -*- +from functools import wraps +from typing import Iterator, TypeVar, Callable + +T = TypeVar("T") + + +def infinite_generator(func: Callable[[...], Iterator[T]]): + @wraps(func) + def wrapper(*args, **kwargs): + items = list(func(*args, **kwargs)) + + while True: + for item in items: + yield item + + 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 from typing import Tuple, Iterator, Set, Any, Optional from aoc import BaseAssignment - -Coordinate = Tuple[int, int] +from aoc.datastructures import Coordinate 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 @@ # -*- coding: utf-8 -*- import re from abc import ABC -from collections import namedtuple from dataclasses import dataclass, field -from typing import Tuple, Iterator, Any, Set, Union -from typing import Tuple, Iterator, Any, Set, List +from typing import Union +from typing import Tuple, Iterator, Set, List from aoc import BaseAssignment - - -class Coordinate(namedtuple("Coordinate", ["x", "y"])): - def distance(self, other: "Coordinate"): - return abs(self.x - other.x) + abs(self.y - other.y) +from aoc.datastructures import Coordinate @dataclass @@ -24,10 +19,10 @@ class Sensor: return hash(self.coordinate) def __post_init__(self): - self.radius = self.coordinate.distance(self.nearest) + self.radius = self.coordinate.manhattan_distance(self.nearest) def within_radius(self, coordinate: Coordinate) -> bool: - distance = self.coordinate.distance(coordinate) + distance = self.coordinate.manhattan_distance(coordinate) return distance <= self.radius 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 @@ +# -*- coding: utf-8 -*- +from abc import ABC +from dataclasses import dataclass +from typing import Iterator + +from aoc import BaseAssignment +from aoc.datastructures import Coordinate +from aoc.decorators import infinite_generator + + +@dataclass +class Block: + origin: Coordinate + + +class Assignment(BaseAssignment[int, str], ABC): + @infinite_generator + def infinite_moves(self, item: str) -> str: + return item + + def infinite_blocks(self): + pass + + +class AssignmentOne(Assignment): + example_result = 1 + + def run(self, input: Iterator[str]): + i = 0 + + moves = self.infinite_moves(input) + + for move in self.infinite_moves(next(input)): + i += 1 + print(move) + + if i == 100: + break + + +class AssignmentTwo(Assignment): + 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 @@ # -*- coding: utf-8 -*- from abc import ABC from copy import copy -from dataclasses import dataclass from typing import Tuple, Iterator, List, Set from aoc import BaseAssignment - - -@dataclass -class Coordinate: - x: int - y: int - - def __hash__(self): - return tuple([self.x, self.y]).__hash__() - - def __sub__(self, other: "Coordinate"): - return Coordinate(self.x - other.x, self.y - other.y) - - @property - def polarity_x(self): - try: - return abs(self.x) / self.x - except ZeroDivisionError: - return 0 - - @property - def polarity_y(self): - try: - return abs(self.y) / self.y - except ZeroDivisionError: - return 0 +from aoc.datastructures import Coordinate class Assignment(BaseAssignment, ABC): @@ -44,36 +18,44 @@ class Assignment(BaseAssignment, ABC): pass @staticmethod - def next_head(head: Coordinate, direction: str): + def next_head(head: Coordinate, direction: str) -> Coordinate: match direction: case "R": - head.x += 1 + return head + Coordinate(1, 0) case "L": - head.x -= 1 + return head + Coordinate(-1, 0) case "U": - head.y += 1 + return head + Coordinate(0, 1) case "D": - head.y -= 1 + return head + Coordinate(0, -1) @staticmethod - def next_knot(head: Coordinate, tail: Coordinate): + def next_knot(head: Coordinate, tail: Coordinate) -> Coordinate: delta = head - tail if abs(delta.x) < 2 and abs(delta.y) < 2: - pass + return tail elif abs(delta.x) > 1 and delta.y == 0: - tail.x += delta.x - delta.polarity_x + return tail + Coordinate( + delta.x - delta.polarity_x, + 0, + ) elif abs(delta.x) > 1 and abs(delta.y) == 1: - tail.x += delta.x - delta.polarity_x - tail.y += delta.y + return tail + Coordinate(delta.x - delta.polarity_x, delta.y) elif abs(delta.y) > 1 and delta.x == 0: - tail.y += delta.y - delta.polarity_y + return tail + Coordinate( + 0, + delta.y - delta.polarity_y, + ) elif abs(delta.y) > 1 and abs(delta.x) == 1: - tail.y += delta.y - delta.polarity_y - tail.x += delta.x + return tail + Coordinate( + delta.x, + delta.y - delta.polarity_y, + ) elif abs(delta.x) > 1 and abs(delta.y) > 1: - tail.x += delta.x - delta.polarity_x - tail.y += delta.y - delta.polarity_y + return tail + Coordinate( + delta.x - delta.polarity_x, delta.y - delta.polarity_y + ) def tail_positions(self, input: Iterator[str], length: int) -> Iterator[Coordinate]: knots = [Coordinate(0, 0) for _ in range(length)] @@ -84,10 +66,10 @@ class Assignment(BaseAssignment, ABC): for _ in range(amount): for index in range(length): if index == 0: - self.next_head(knots[index], direction) + knots[0] = self.next_head(knots[0], direction) continue - self.next_knot(knots[index - 1], knots[index]) + knots[index] = self.next_knot(knots[index - 1], knots[index]) yield knots[length - 1] -- cgit v1.2.3