-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_19.py
186 lines (166 loc) · 5.66 KB
/
day_19.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
from collections import defaultdict
import aoc_helper
from aoc_helper import (
decode_text,
extract_ints,
frange,
irange,
iter,
list,
map,
range,
tail_call,
)
raw = aoc_helper.fetch(19, 2021)
def parse_raw():
reports = raw.split("\n\n")
return list(
set(tuple(extract_ints(line)) for line in report.splitlines()[1:])
for report in reports
)
data = parse_raw()
def rotate(facing, up, point):
new_point = list([0, 0, 0])
match facing:
case "+x":
new_point[0] = point[0]
match up:
case "+y":
new_point[1] = point[1]
new_point[2] = point[2]
case "-y":
new_point[1] = -point[1]
new_point[2] = -point[2]
case "+z":
new_point[1] = point[2]
new_point[2] = -point[1]
case "-z":
new_point[1] = -point[2]
new_point[2] = point[1]
case "-x":
new_point[0] = -point[0]
match up:
case "+y":
new_point[1] = point[1]
new_point[2] = -point[2]
case "-y":
new_point[1] = -point[1]
new_point[2] = point[2]
case "+z":
new_point[1] = point[2]
new_point[2] = point[1]
case "-z":
new_point[1] = -point[2]
new_point[2] = -point[1]
case "+y":
new_point[0] = point[1]
match up:
case "+x":
new_point[1] = point[0]
new_point[2] = -point[2]
case "-x":
new_point[1] = -point[0]
new_point[2] = point[2]
case "+z":
new_point[1] = point[2]
new_point[2] = point[0]
case "-z":
new_point[1] = -point[2]
new_point[2] = -point[0]
case "-y":
new_point[0] = -point[1]
match up:
case "+x":
new_point[1] = point[0]
new_point[2] = point[2]
case "-x":
new_point[1] = -point[0]
new_point[2] = -point[2]
case "+z":
new_point[1] = point[2]
new_point[2] = -point[0]
case "-z":
new_point[1] = -point[2]
new_point[2] = point[0]
case "+z":
new_point[0] = point[2]
match up:
case "+x":
new_point[1] = point[0]
new_point[2] = point[1]
case "-x":
new_point[1] = -point[0]
new_point[2] = -point[1]
case "+y":
new_point[1] = point[1]
new_point[2] = -point[0]
case "-y":
new_point[1] = -point[1]
new_point[2] = point[0]
case "-z":
new_point[0] = -point[2]
match up:
case "+x":
new_point[1] = point[0]
new_point[2] = -point[1]
case "-x":
new_point[1] = -point[0]
new_point[2] = point[1]
case "+y":
new_point[1] = point[1]
new_point[2] = point[0]
case "-y":
new_point[1] = -point[1]
new_point[2] = -point[0]
return tuple(new_point)
def translate(point, off):
return point[0] + off[0], point[1] + off[1], point[2] + off[2]
def untranslate(src, dest):
return src[0] - dest[0], src[1] - dest[1], src[2] - dest[2]
rotations = [
(sa + da, sb + db)
for sa in "+-"
for sb in "+-"
for da in "xyz"
for db in "xyz"
if db != da
]
def common_with_offset(reference, new, offset):
return len(set(reference) & {translate(beacon, offset) for beacon in new})
def report_partial_match(reference, new):
for reference_beacon in reference:
for new_beacon in new:
offset = untranslate(reference_beacon, new_beacon)
if common_with_offset(reference, new, offset) >= 12:
return offset
def report_match(reference, new):
for facing, up in rotations:
transformed = {rotate(facing,up,point) for point in new}
found = report_partial_match(reference, transformed)
if found:
return {translate(point, found) for point in transformed}, found
def match_one(reports, fixed_reports, matched):
for i, report in enumerate(reports):
for other_report, _ in fixed_reports:
found = report_match(other_report, report)
if found:
beacons, _ = found
matched |= beacons
fixed_reports.append(found)
reports.pop(i)
return
def match(reports):
matched, *to_match = reports
fixed_reports = [(matched, (0,0,0))]
matched = matched.copy()
while to_match:
print('finding match:',len(to_match),'left')
match_one(to_match,fixed_reports,matched)
return matched, fixed_reports
beacons, reports = match(data)
def part_one():
return len(beacons)
def part_two():
return max(sum(map(abs,untranslate(scanner_a, scanner_b))) for _, scanner_a in reports for _, scanner_b in reports)
aoc_helper.lazy_submit(day=19, year=2021, solution=part_one)
aoc_helper.lazy_submit(day=19, year=2021, solution=part_two)