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