-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.js
241 lines (212 loc) · 6.85 KB
/
day19.js
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
241
const { test, data } = require('./data/day19');
const SCANNER_LINE = /--- scanner (\d+) ---/;
const parseInput = (input) => {
let scanners = [];
let currentScanner;
for (element of input) {
const matchesNextScanner = element.match(SCANNER_LINE);
if (matchesNextScanner) {
scanners.push([]);
currentScanner = scanners.length - 1;
continue;
}
scanners[currentScanner].push(element.split(',').map(Number));
}
return scanners;
};
const rotations = [
([x, y, z]) => [x, y, z],
([x, y, z]) => [x, z, -y],
([x, y, z]) => [x, -y, -z],
([x, y, z]) => [x, -z, y],
([x, y, z]) => [-x, y, -z],
([x, y, z]) => [-x, -y, z],
([x, y, z]) => [-x, z, y],
([x, y, z]) => [-x, -z, -y],
([x, y, z]) => [y, z, x],
([x, y, z]) => [y, -x, z],
([x, y, z]) => [y, x, -z],
([x, y, z]) => [y, -z, -x],
([x, y, z]) => [-y, x, z],
([x, y, z]) => [-y, z, -x],
([x, y, z]) => [-y, -z, x],
([x, y, z]) => [-y, -x, -z],
([x, y, z]) => [z, x, y],
([x, y, z]) => [z, y, -x],
([x, y, z]) => [z, -y, x],
([x, y, z]) => [z, -x, -y],
([x, y, z]) => [-z, y, x],
([x, y, z]) => [-z, -x, y],
([x, y, z]) => [-z, x, -y],
([x, y, z]) => [-z, -y, -x],
];
const rotate = (scanner, rotation) => scanner.map(rotation);
const translate = (scanner, translation) => scanner.map((beacon) => (
beacon.map((coord, idx) => coord + translation[idx])
));
const transform = (scanner, { rotation, translation }) => (
translate(rotate(scanner, rotation), translation)
);
/**
* Algorithm: create transform list from scanners
*
* Idea:
* For each scanner, i, for each rotation, r, apply r to each of i's beacons, bi -> bi*
* For each other scanner, j, compute distance vectors for each beacon in bi* and bj
* if 12 similar distance vectors exist
* rot is the rotation from i to j, and the distance vector is the translation
* Now, we have all of the pair-wise scanner overlap regions,
* but not all regions are directly connected to origin,
* so we need to find connected transforms to walk back to origin
*
* transforms = [
* [fromIdx (this scanner)]: {
* [toKey]: [
* { rotation, translation }
* ]
* }
* ]
*
* // Get each scanner's overlaps
* for each scanner, i
* for each other scanner, j
* for each rotation, rot
* apply rot to scanner i => scanner i*
* for each beacon b1* in scanner i*
* for each beacon b2 in scanner j
* compute each coordinate differences b2 - b1*
* add count to difference vector
* if have 12 matching difference vectors
* store rotation and difference vector in as first element in transform[i][j] array
* -- transform arrays will be how we apply transforms from disconnected observation cubes to reach 0
* move to next j for this i
*
* // Get each scanner's to-zero
* while there's a transform [from] without a [to] 0
* for each transform without a [to] 0, j
* for each transform of j, k
* if k has a transform [to] 0
* set j's transform [to] 0 to be j->k->0
* we're done this path for now, break inner-most loop
*/
const findTransforms = (scanners) => {
const transforms = scanners.map(() => ({}));
for (let i = 0; i < scanners.length; i++) {
if (i === 0) {
// set scanner zero as the origin rotation
transforms[i] = {
[i]: [
{
rotation: rotations[i],
translation: [0, 0, 0],
},
],
};
continue;
}
// for each scanner, i
const firstScanner = scanners[i];
innerLoop:
for (let j = 0; j < scanners.length; j++) {
if (i === j) {
continue;
}
// for each other scanner, j
const secondScanner = scanners[j];
for (const rotation of rotations) {
// for each rotation, rot
const differences = {};
for (const beacon1 of firstScanner) {
// apply rot to scanner i => scanner i*
const [x1, y1, z1] = rotation(beacon1);
// for each beacon b1* in scanner i*
for (const beacon2 of secondScanner) {
// for each beacon b2 in scanner j
const [x2, y2, z2] = beacon2;
// compute each coordinate differences b2 - b1*
const diff = [x2 - x1, y2 - y1, z2 - z1];
const diffStr = diff.join();
// add count to difference vector
if (!differences[diffStr]) {
differences[diffStr] = 0;
}
differences[diffStr] += 1;
if (differences[diffStr] === 12) {
// store rotation and difference vector in as first element in transform[i][j] array
transforms[i][j] = [
{
rotation,
translation: diff,
},
];
// move to next j for this i
continue innerLoop;
}
}
}
}
}
}
// while there's a transform [from] without a [to] 0
while (transforms.some(t => typeof t[0] === 'undefined')) {
// for each transform without a [to] 0, j
for (let i = 1; i < transforms.length; i++) {
if (transforms[i][0]) {
continue;
}
// for each transform of j, k
for (let j in transforms[i]) {
// if k has a transform [to] 0
if (typeof transforms[j][0] === 'undefined') {
continue;
}
// set j's transform [to] 0 to be j->k->0
transforms[i][0] = transforms[i][j].concat(transforms[j][0]);
// we're done this path for now, break inner-most loop
break;
}
}
}
return transforms;
}
const findBeacons = (scanners) => {
const transforms = findTransforms(scanners);
const beacons = scanners
.map((scanner, idx) => (
transforms[idx][0]
.reduce(transform, scanner)
.map((beacon) => beacon.join(','))
))
.reduce((agg, curr) => ({
...agg,
...curr.reduce((els, el) => ({ ...els, [el]: true }), {}),
}), {});
return { beacons, transforms };
};
const solve = (scanners) => {
const { beacons, transforms } = findBeacons(scanners);
const numBeacons = Object.keys(beacons).length;
const scannerCoords = transforms.reduce((coords, transformBag, scannerIdx) => [
...coords,
transformBag[0].reduce(transform, [[0, 0, 0]]),
], []).map(([el]) => el);
let maxDistance = -Infinity;
scannerCoords.forEach((first) => {
scannerCoords.forEach((second) => {
const currentDist = Math.abs(first[0] - second[0]) + Math.abs(first[1] - second[1]) + Math.abs(first[2] - second[2]);
if (currentDist > maxDistance) {
maxDistance = currentDist;
}
});
});
return {
numBeacons,
maxDistance,
};
};
const parsedTest = parseInput(test);
const parsedData = parseInput(data);
console.log({
test: solve(parsedTest),
data: solve(parsedData),
});