-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.py
238 lines (191 loc) · 6.16 KB
/
day19.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import argparse
import os
from collections import Counter, deque
from collections.abc import Callable
from dataclasses import dataclass
from functools import cache, reduce
from itertools import combinations
UNIQUE_ROTATIONS = (
"I",
"X",
"Y",
"Z",
"XX",
"XY",
"XZ",
"YX",
"YY",
"ZY",
"ZZ",
"XXX",
"XXY",
"XXZ",
"XYX",
"XYY",
"XZZ",
"YXX",
"YYY",
"ZZZ",
"XXXY",
"XXYX",
"XYXX",
"XYYY",
)
@cache
def rotation_factory(rotations: str) -> Callable:
"""
Take a sequence of rotation axes and returns a function that performs
those rotations.
Possible rotations 'x', 'y', 'z'
Usage: rotation_factory('xxyz')
returns a function to rotate a beacon twice around x,
once around y and once around z
"""
rotation_map = {
"i": lambda b: b,
"x": lambda b: Beacon(b.x, -b.z, b.y),
"y": lambda b: Beacon(b.z, b.y, -b.x),
"z": lambda b: Beacon(-b.y, b.x, b.z),
}
def compose(f, g):
return lambda x: f(g(x))
functions = [rotation_map[r] for r in rotations.lower()]
return reduce(compose, functions, lambda b: b)
@dataclass(frozen=True)
class Beacon:
x: int = 0
y: int = 0
z: int = 0
def __repr__(self):
return f"({self.x}, {self.y}, {self.z})"
def translate(self, reference: "Beacon") -> "Beacon":
return Beacon(reference.x - self.x, reference.y - self.y, reference.z - self.z)
def add(self, other: "Beacon") -> "Beacon":
return Beacon(self.x + other.x, self.y + other.y, self.z + other.z)
def sub(self, other: "Beacon") -> "Beacon":
return Beacon(self.x - other.x, self.y - other.y, self.z - other.z)
@dataclass(frozen=True)
class Orientation:
rotation: str = ""
translation: Beacon = Beacon()
def __repr__(self):
return f"O(r={self.rotation}, t={self.translation}"
@dataclass
class Scanner:
id: int
beacons: set[Beacon]
match: "Scanner" = None
orientation: Orientation = Orientation()
center = Beacon()
def __repr__(self):
return (
f"Scanner(id={self.id}, match={self.match.id if self.match else 'None'}, "
f"o={self.orientation})"
)
def rotate(self, rotations: str) -> set[Beacon]:
return {rotation_factory(rotations)(b) for b in self.beacons}
def apply_orientation(self, o: Orientation = None) -> set[Beacon]:
orientation = o if o is not None else self.orientation
rotated = self.rotate(orientation.rotation)
translated = {b.add(orientation.translation) for b in rotated}
return translated
def distance_signature(self) -> dict[int, (Beacon, Beacon)]:
self.relative_beacons = self.beacons.copy()
sig = dict()
for b in self.beacons:
for a in self.beacons:
sig[mdistance(b, a)] = (b, a)
return sig
def mdistance(b0: Beacon, b1: Beacon) -> int:
return abs(b0.x - b1.x) + abs(b0.y - b1.y) + abs(b0.z - b1.z)
def mdistance_between_scanners(s0: Scanner, s1: Scanner) -> int:
b0 = s0.center
b1 = s1.center
return mdistance(b0, b1)
def transform_beacons(beacons: set[Beacon], orientation: Orientation) -> set[Beacon]:
return {
rotation_factory(orientation.rotation)(b).add(orientation.translation)
for b in beacons
}
def match_overlaping_scanner(scanners: list[Scanner]):
q = deque(scanners)
origin = q.popleft()
locked = [origin]
while q:
found = False
s = q.popleft()
signature = s.distance_signature()
for scanner in locked:
s1dist = scanner.distance_signature()
overlap = set(s1dist).intersection(set(signature))
if len(overlap) < 66:
continue
locked.append(s)
s.match = scanner
found = True
break
if not found:
q.append(s)
def best_translation(b0: set[Beacon], b1: set[Beacon]) -> (Beacon, int):
c = Counter()
for x in b1:
for y in b0:
t = x.translate(y)
c[t] += 1
return c.most_common(1)[0]
def match_scanners(s: Scanner):
if s.match is None:
return
Counter()
for rotation in UNIQUE_ROTATIONS:
r = s.rotate(rotation)
t = best_translation(s.match.beacons, r)
if t[1] >= 12:
s.orientation = Orientation(rotation, t[0])
def scanner_to_origin(s: Scanner) -> set[Beacon]:
beacons = transform_beacons(s.beacons, s.orientation)
center = transform_beacons({s.center}, s.orientation)
next_ = s.match
while next_:
if next_.match is None:
break
beacons = transform_beacons(beacons, next_.orientation)
center = transform_beacons(center, next_.orientation)
next_ = next_.match
s.center = center.pop()
return beacons
def read_input(filepath: str) -> list[Scanner]:
output = list()
scanner = 0
with open(filepath, "r") as f:
for line in (l.strip() for l in f.readlines()):
if line.startswith("---"):
beacons = set()
elif line != "":
point = [int(x) for x in line.split(",")]
beacons.add(Beacon(point[0], point[1], point[2]))
else:
output.append(Scanner(scanner, beacons))
scanner += 1
output.append(Scanner(scanner, beacons))
return output
def init_parser() -> str:
parser = argparse.ArgumentParser(description="Advent of Code day 19 solution.")
parser.add_argument(
"input", metavar="FILE", type=str, nargs=1, help="Path to input data."
)
args = parser.parse_args()
return os.path.realpath(args.input[0])
if __name__ == "__main__":
path = init_parser()
scanners = read_input(path)
match_overlaping_scanner(scanners)
[match_scanners(s) for s in scanners]
unique = reduce(
lambda a, b: a.union(b), [scanner_to_origin(s) for s in scanners], set()
)
print(f"Part 1: {len(unique)}")
distances = [mdistance_between_scanners(x, y) for x, y in combinations(scanners, 2)]
print(f"Part 2: {max(distances)}")
def main(_):
raise NotImplementedError