2021 - Day 19 (Beacon Scanner)
View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3
#Advent of Code
#2021 Day 19, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
def solve(puzzle_input):
class Scanner:
def __init__(self, beacons):
self.beacons = beacons
self.final = set()
def xy_rotations(self):
yield lambda x, y, z: (x, y, z)
yield lambda x, y, z: (y, -x, z)
yield lambda x, y, z: (-x, -y, z)
yield lambda x, y, z: (-y, x, z)
def yz_rotations(self):
yield lambda x, y, z: (x, y, z)
yield lambda x, y, z: (x, z, -y)
yield lambda x, y, z: (x, -y, -z)
yield lambda x, y, z: (x, -z, y)
def xz_rotations(self):
yield lambda x, y, z: (x, y, z)
yield lambda x, y, z: (z, y, -x)
yield lambda x, y, z: (-x, y, -z)
yield lambda x, y, z: (-z, y, x)
def rotations(self):
for xy in self.xy_rotations():
for yz in self.yz_rotations():
for xz in self.xz_rotations():
rotated_beacons = set()
for beacon in self.beacons:
rotated_beacons.add(xz(*yz(*xy(*beacon))))
yield rotated_beacons
def translate(self, beacons, offset):
return set([(beacon[0] + offset[0], beacon[1] + offset[1], beacon[2] + offset[2]) for beacon in beacons])
scanners = []
beacons = set()
for line in puzzle_input:
if line.startswith("---"):
continue
elif line == "":
scanners.append(Scanner(beacons))
beacons = set()
else:
points = [int(n) for n in line.split(",")]
beacons.add(tuple(points))
scanners.append(Scanner(beacons))
fixed = set()
fixed.add(scanners[0])
scanners[0].final = scanners[0].beacons
while len(fixed) != len(scanners):
for scanner in scanners:
if scanner in fixed:
continue
if len(fixed) == 1:
fixed_beacons = scanners[0].final
else:
fixed_beacons = scanners[0].final.union(*[s.final for s in fixed if s is not scanners[0]])
for r in scanner.rotations():
for fb in fixed_beacons:
for sb in r:
offset = (fb[0] - sb[0], fb[1] - sb[1], fb[2] - sb[2])
shifted = scanner.translate(r, offset)
if len(shifted & fixed_beacons) >= 12:
scanner.final = shifted
fixed.add(scanner)
break
return len(scanners[0].final.union(*[s.final for s in scanners[1:]]))
def main():
puzzle_input = util.read.as_lines()
beacons = solve(puzzle_input)
print("The number of beacons is " + str(beacons) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["--- scanner 0 ---",
"404,-588,-901",
"528,-643,409",
"-838,591,734",
"390,-675,-793",
"-537,-823,-458",
"-485,-357,347",
"-345,-311,381",
"-661,-816,-575",
"-876,649,763",
"-618,-824,-621",
"553,345,-567",
"474,580,667",
"-447,-329,318",
"-584,868,-557",
"544,-627,-890",
"564,392,-477",
"455,729,728",
"-892,524,684",
"-689,845,-530",
"423,-701,434",
"7,-33,-71",
"630,319,-379",
"443,580,662",
"-789,900,-551",
"459,-707,401",
"",
"--- scanner 1 ---",
"686,422,578",
"605,423,415",
"515,917,-361",
"-336,658,858",
"95,138,22",
"-476,619,847",
"-340,-569,-846",
"567,-361,727",
"-460,603,-452",
"669,-402,600",
"729,430,532",
"-500,-761,534",
"-322,571,750",
"-466,-666,-811",
"-429,-592,574",
"-355,545,-477",
"703,-491,-529",
"-328,-685,520",
"413,935,-424",
"-391,539,-444",
"586,-435,557",
"-364,-763,-893",
"807,-499,-711",
"755,-354,-619",
"553,889,-390",
"",
"--- scanner 2 ---",
"649,640,665",
"682,-795,504",
"-784,533,-524",
"-644,584,-595",
"-588,-843,648",
"-30,6,44",
"-674,560,763",
"500,723,-460",
"609,671,-379",
"-555,-800,653",
"-675,-892,-343",
"697,-426,-610",
"578,704,681",
"493,664,-388",
"-671,-858,530",
"-667,343,800",
"571,-461,-707",
"-138,-166,112",
"-889,563,-600",
"646,-828,498",
"640,759,510",
"-630,509,768",
"-681,-892,-333",
"673,-379,-804",
"-742,-814,-386",
"577,-820,562",
"",
"--- scanner 3 ---",
"-589,542,597",
"605,-692,669",
"-500,565,-823",
"-660,373,557",
"-458,-679,-417",
"-488,449,543",
"-626,468,-788",
"338,-750,-386",
"528,-832,-391",
"562,-778,733",
"-938,-730,414",
"543,643,-506",
"-524,371,-870",
"407,773,750",
"-104,29,83",
"378,-903,-323",
"-778,-728,485",
"426,699,580",
"-438,-605,-362",
"-469,-447,-387",
"509,732,623",
"647,635,-688",
"-868,-804,481",
"614,-800,639",
"595,780,-596",
"",
"--- scanner 4 ---",
"727,592,562",
"-293,-554,779",
"441,611,-461",
"-714,465,-776",
"-743,427,-804",
"-660,-479,-426",
"832,-632,460",
"927,-485,-438",
"408,393,-506",
"466,436,-512",
"110,16,151",
"-258,-428,682",
"-393,719,612",
"-211,-452,876",
"808,-476,-593",
"-575,615,604",
"-485,667,467",
"-680,325,-822",
"-627,-443,-432",
"872,-547,-609",
"833,512,582",
"807,604,487",
"839,-516,451",
"891,-625,532",
"-652,-548,-490",
"30,-46,-14"]), 79)
if __name__ == "__main__":
run(main)
Part 2
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
239
240
#!/usr/bin/env python3
#Advent of Code
#2021 Day 19, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import combinations
def solve(puzzle_input):
class Scanner:
def __init__(self, beacons):
self.beacons = beacons
self.final = set()
def xy_rotations(self):
yield lambda x, y, z: (x, y, z)
yield lambda x, y, z: (y, -x, z)
yield lambda x, y, z: (-x, -y, z)
yield lambda x, y, z: (-y, x, z)
def yz_rotations(self):
yield lambda x, y, z: (x, y, z)
yield lambda x, y, z: (x, z, -y)
yield lambda x, y, z: (x, -y, -z)
yield lambda x, y, z: (x, -z, y)
def xz_rotations(self):
yield lambda x, y, z: (x, y, z)
yield lambda x, y, z: (z, y, -x)
yield lambda x, y, z: (-x, y, -z)
yield lambda x, y, z: (-z, y, x)
def rotations(self):
for xy in self.xy_rotations():
for yz in self.yz_rotations():
for xz in self.xz_rotations():
rotated_beacons = set()
for beacon in self.beacons:
rotated_beacons.add(xz(*yz(*xy(*beacon))))
yield rotated_beacons
def translate(self, beacons, offset):
return set([(beacon[0] + offset[0], beacon[1] + offset[1], beacon[2] + offset[2]) for beacon in beacons])
scanners = []
beacons = set()
for line in puzzle_input:
if line.startswith("---"):
continue
elif line == "":
scanners.append(Scanner(beacons))
beacons = set()
else:
points = [int(n) for n in line.split(",")]
beacons.add(tuple(points))
scanners.append(Scanner(beacons))
fixed = set()
fixed.add(scanners[0])
scanners[0].final = scanners[0].beacons
scanners[0].offset = (0, 0, 0)
while len(fixed) != len(scanners):
for scanner in scanners:
if scanner in fixed:
continue
if len(fixed) == 1:
fixed_beacons = scanners[0].final
else:
fixed_beacons = scanners[0].final.union(*[s.final for s in fixed if s is not scanners[0]])
for r in scanner.rotations():
for fb in fixed_beacons:
for sb in r:
offset = (fb[0] - sb[0], fb[1] - sb[1], fb[2] - sb[2])
shifted = scanner.translate(r, offset)
if len(shifted & fixed_beacons) >= 12:
scanner.final = shifted
scanner.offset = offset
fixed.add(scanner)
break
def manhattan_distance(p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) + abs(p1[2] - p2[2])
return max(manhattan_distance(a, b) for a, b in combinations([s.offset for s in scanners], 2))
def main():
puzzle_input = util.read.as_lines()
distance = solve(puzzle_input)
print("The largest Manhattan distance between any two scanners is " + str(distance) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["--- scanner 0 ---",
"404,-588,-901",
"528,-643,409",
"-838,591,734",
"390,-675,-793",
"-537,-823,-458",
"-485,-357,347",
"-345,-311,381",
"-661,-816,-575",
"-876,649,763",
"-618,-824,-621",
"553,345,-567",
"474,580,667",
"-447,-329,318",
"-584,868,-557",
"544,-627,-890",
"564,392,-477",
"455,729,728",
"-892,524,684",
"-689,845,-530",
"423,-701,434",
"7,-33,-71",
"630,319,-379",
"443,580,662",
"-789,900,-551",
"459,-707,401",
"",
"--- scanner 1 ---",
"686,422,578",
"605,423,415",
"515,917,-361",
"-336,658,858",
"95,138,22",
"-476,619,847",
"-340,-569,-846",
"567,-361,727",
"-460,603,-452",
"669,-402,600",
"729,430,532",
"-500,-761,534",
"-322,571,750",
"-466,-666,-811",
"-429,-592,574",
"-355,545,-477",
"703,-491,-529",
"-328,-685,520",
"413,935,-424",
"-391,539,-444",
"586,-435,557",
"-364,-763,-893",
"807,-499,-711",
"755,-354,-619",
"553,889,-390",
"",
"--- scanner 2 ---",
"649,640,665",
"682,-795,504",
"-784,533,-524",
"-644,584,-595",
"-588,-843,648",
"-30,6,44",
"-674,560,763",
"500,723,-460",
"609,671,-379",
"-555,-800,653",
"-675,-892,-343",
"697,-426,-610",
"578,704,681",
"493,664,-388",
"-671,-858,530",
"-667,343,800",
"571,-461,-707",
"-138,-166,112",
"-889,563,-600",
"646,-828,498",
"640,759,510",
"-630,509,768",
"-681,-892,-333",
"673,-379,-804",
"-742,-814,-386",
"577,-820,562",
"",
"--- scanner 3 ---",
"-589,542,597",
"605,-692,669",
"-500,565,-823",
"-660,373,557",
"-458,-679,-417",
"-488,449,543",
"-626,468,-788",
"338,-750,-386",
"528,-832,-391",
"562,-778,733",
"-938,-730,414",
"543,643,-506",
"-524,371,-870",
"407,773,750",
"-104,29,83",
"378,-903,-323",
"-778,-728,485",
"426,699,580",
"-438,-605,-362",
"-469,-447,-387",
"509,732,623",
"647,635,-688",
"-868,-804,481",
"614,-800,639",
"595,780,-596",
"",
"--- scanner 4 ---",
"727,592,562",
"-293,-554,779",
"441,611,-461",
"-714,465,-776",
"-743,427,-804",
"-660,-479,-426",
"832,-632,460",
"927,-485,-438",
"408,393,-506",
"466,436,-512",
"110,16,151",
"-258,-428,682",
"-393,719,612",
"-211,-452,876",
"808,-476,-593",
"-575,615,604",
"-485,667,467",
"-680,325,-822",
"-627,-443,-432",
"872,-547,-609",
"833,512,582",
"807,604,487",
"839,-516,451",
"891,-625,532",
"-652,-548,-490",
"30,-46,-14"]), 3621)
if __name__ == "__main__":
run(main)