summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--conftest.py4
-rw-r--r--day12/__init__.py5
-rw-r--r--day13/__init__.py109
-rw-r--r--day13/example.txt21
-rw-r--r--day13/input.txt996
5 files changed, 1127 insertions, 8 deletions
diff --git a/conftest.py b/conftest.py
index c846afa..24884e2 100644
--- a/conftest.py
+++ b/conftest.py
@@ -21,14 +21,12 @@ def pytest_generate_tests(metafunc: Metafunc):
21 for part in ['1', '2'] 21 for part in ['1', '2']
22 ] 22 ]
23 23
24 print(assignments)
25
26 metafunc.parametrize( 24 metafunc.parametrize(
27 argnames=f'assignment', 25 argnames=f'assignment',
28 argvalues=[ 26 argvalues=[
29 Assignment(path=package.__path__[0]) 27 Assignment(path=package.__path__[0])
30 for (Assignment, package) in assignments 28 for (Assignment, package) in assignments
31 if Assignment is not None and hasattr(package, '__path__') 29 if Assignment is not None and hasattr(package, '__path__') and Assignment.example_result != NotImplemented
32 ], 30 ],
33 ids=lambda assignment: str(assignment) 31 ids=lambda assignment: str(assignment)
34 ) 32 )
diff --git a/day12/__init__.py b/day12/__init__.py
index aad531d..9d7a036 100644
--- a/day12/__init__.py
+++ b/day12/__init__.py
@@ -108,9 +108,4 @@ class AssignmentTwo(Assignment):
108 108
109 def run(self, input: Dict[str, Node]) -> int: 109 def run(self, input: Dict[str, Node]) -> int:
110 paths = sorted(self.calculate_all_paths(input['start'], input['end'])) 110 paths = sorted(self.calculate_all_paths(input['start'], input['end']))
111
112 print()
113 for path in paths:
114 print(','.join([ n.id for n in path ]))
115
116 return len(paths) 111 return len(paths)
diff --git a/day13/__init__.py b/day13/__init__.py
new file mode 100644
index 0000000..41b81dc
--- /dev/null
+++ b/day13/__init__.py
@@ -0,0 +1,109 @@
1import re
2from abc import ABC
3from copy import copy
4from dataclasses import dataclass
5from typing import List, Tuple, Union, TypedDict
6
7from aoc import BaseAssignment
8
9Coordinate = Tuple[int, int]
10
11def mirrored_index(index: int, fold: int) -> int:
12 return fold + (fold - index)
13
14@dataclass
15class Sheet:
16 dots: List[Coordinate]
17 width: int = 0
18 height: int = 0
19
20 def __post_init__(self):
21 x_coordinates, y_coordinates = zip(*self.dots)
22
23 self.width = max(x_coordinates) + 1
24 self.height = max(y_coordinates) + 1
25
26
27 def fold_x(self, position: int):
28 for x, y in copy(self.dots):
29 mirrored_x = mirrored_index(x, position)
30 if mirrored_x < position:
31 self.dots.append((mirrored_x, y))
32 self.dots.remove((x, y))
33
34 self.width = self.width // 2
35 self.dots = list(set(self.dots))
36
37 def fold_y(self, position: int):
38 for x, y in copy(self.dots):
39 mirrored_y = mirrored_index(y, position)
40 if mirrored_y < position:
41 self.dots.append((x, mirrored_y))
42 self.dots.remove((x, y))
43
44 self.height = self.height // 2
45 self.dots = list(set(self.dots))
46
47 def __repr__(self):
48 dots = set(self.dots)
49 return '\n'.join([
50 '',
51 *[
52 ''.join([
53 '█' if (x, y) in dots else ' '
54 for x in range(self.width)
55 ])
56 for y in range(self.height)
57 ],
58 '',
59 ])
60
61class Instruction(TypedDict):
62 direction: str
63 position: int
64
65coordinate_regex = re.compile('(\d+),(\d+)')
66instruction_regex = re.compile('fold along (?P<direction>[xy])=(?P<position>\d+)')
67
68class Assignment(BaseAssignment, ABC):
69 def parse_item(self, item: str) -> Union[Coordinate,Instruction]:
70 if coordinate := coordinate_regex.match(item):
71 return tuple(int(i) for i in coordinate.groups())
72
73 if instruction := instruction_regex.match(item):
74 return {
75 **instruction.groupdict(),
76 'position': int(instruction.groupdict()['position'])
77 }
78
79 def read_input(self, example = False) -> Tuple[Sheet, List[Instruction]]:
80 input = super().read_input(example)
81
82 coordinates: List[Coordinate] = []
83
84 while True:
85 coordinate = next(input)
86 if coordinate is None:
87 break
88
89 coordinates.append(coordinate)
90
91 sheet = Sheet(dots=coordinates)
92
93 return sheet, list(input)
94
95
96class AssignmentOne(Assignment):
97 example_result = 17
98
99 def run(self, input: Tuple[Sheet, List[Instruction]]) -> int:
100 sheet, instructions = input
101 getattr(sheet, f'fold_{instructions[0]["direction"]}')(instructions[0]["position"])
102 return len(sheet.dots)
103
104class AssignmentTwo(Assignment):
105 def run(self, input: Tuple[Sheet, List[Instruction]]) -> Sheet:
106 sheet, instructions = input
107 for instruction in instructions:
108 getattr(sheet, f'fold_{instruction["direction"]}')(instruction["position"])
109 return sheet
diff --git a/day13/example.txt b/day13/example.txt
new file mode 100644
index 0000000..282114c
--- /dev/null
+++ b/day13/example.txt
@@ -0,0 +1,21 @@
16,10
20,14
39,10
40,3
510,4
64,11
76,0
86,12
94,1
100,13
1110,12
123,4
133,0
148,4
151,10
162,14
178,10
189,0
19
20fold along y=7
21fold along x=5
diff --git a/day13/input.txt b/day13/input.txt
new file mode 100644
index 0000000..0c5500b
--- /dev/null
+++ b/day13/input.txt
@@ -0,0 +1,996 @@
1381,143
2954,841
31061,183
4654,247
530,192
672,215
71223,680
8512,367
9150,747
1085,890
11495,127
12714,710
1331,238
14242,878
151218,772
16862,654
171190,710
18112,229
19276,679
201225,240
211104,654
221120,175
23266,690
24142,79
25977,639
26626,203
271134,147
28898,206
29438,731
3075,800
31731,576
32932,287
33283,236
34659,222
35241,751
36682,182
37305,612
38651,218
39152,491
40890,100
41788,823
421198,229
43842,189
441283,789
45887,113
4617,679
4792,122
48831,21
49894,637
50803,52
511235,36
52373,669
53512,444
54864,376
55184,316
56104,822
57303,340
5882,289
59465,878
60890,794
611041,29
62925,116
63847,553
64905,330
65296,382
66708,387
671255,718
68530,669
69977,460
701153,568
7130,30
72350,740
73872,781
74944,893
751069,143
76438,427
771222,425
78925,555
791034,215
80