summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--aoc/datastructures.py27
-rw-r--r--aoc/decorators.py17
-rw-r--r--day14/__init__.py3
-rw-r--r--day15/__init__.py15
-rw-r--r--day17/__init__.py42
-rw-r--r--day17/example.txt1
-rw-r--r--day17/input.txt1
-rw-r--r--day9/__init__.py70
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 -*-
2from collections import namedtuple
3
4
5class 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 -*-
2from functools import wraps
3from typing import Iterator, TypeVar, Callable
4
5T = TypeVar("T")
6
7
8def 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
4from typing import Tuple, Iterator, Set, Any, Optional 4from typing import Tuple, Iterator, Set, Any, Optional
5 5
6from aoc import BaseAssignment 6from aoc import BaseAssignment
7 7from aoc.datastructures import Coordinate
8Coordinate = Tuple[int, int]
9 8
10 9
11class Assignment(BaseAssignment, ABC): 10class 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 -*-
2import re 2import re
3from abc import ABC 3from abc import ABC
4from collections import namedtuple
5from dataclasses import dataclass, field 4from dataclasses import dataclass, field
6from typing import Tuple, Iterator, Any, Set, Union 5from typing import Union
7from typing import Tuple, Iterator, Any, Set, List 6from typing import Tuple, Iterator, Set, List
8 7
9from aoc import BaseAssignment 8from aoc import BaseAssignment
10 9from aoc.datastructures import Coordinate
11
12class 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 -*-
2from abc import ABC
3from dataclasses import dataclass
4from typing import Iterator
5
6from aoc import BaseAssignment
7from aoc.datastructures import Coordinate
8from aoc.decorators import infinite_generator
9
10
11@dataclass
12class Block:
13 origin: Coordinate
14
15
16class 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
25class 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
41class 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 -*-
2from abc import ABC 2from abc import ABC
3from copy import copy 3from copy import copy
4from dataclasses import dataclass
5from typing import Tuple, Iterator, List, Set 4from typing import Tuple, Iterator, List, Set
6 5
7from aoc import BaseAssignment 6from aoc import BaseAssignment
8 7from aoc.datastructures import Coordinate
9
10@dataclass
11class 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
36class Assignment(BaseAssignment, ABC): 10class 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