summaryrefslogtreecommitdiffstats
path: root/day13
diff options
context:
space:
mode:
Diffstat (limited to 'day13')
-rw-r--r--day13/__init__.py109
-rw-r--r--day13/example.txt21
-rw-r--r--day13/input.txt996
3 files changed, 1126 insertions, 0 deletions
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
80547,379
811233,567
82982,373
831054,754
84110,508
85214,550
86291,505
871036,163
881089,834
891223,214
90569,415
911179,534
92529,578
93206,654
941082,551
95495,655
96353,871
97528,838
98224,431
99380,283
100455,505
1011168,130
102626,483
103687,758
1043,672
10555,340
106907,599
107124,369
108328,73
1091123,396
110194,171
111903,892
11280,660
113277,184
1141233,631
115736,821
11613,750
117562,502
1181307,373
119447,65
120654,763
12143,130
122781,578