-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.js
220 lines (185 loc) · 5.46 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
'use strict' // day19 - interpreting scanner data.
const { findRotation, getOffset, reverse, rotate, shift } = require('./day19rotations')
const { assert, loadData, parseInt } = require('./core/utils')
const { intersection } = require('lodash')
const { abs, max } = Math
const rawInput = [loadData(module.filename), loadData(module.filename, 'demo')]
const parse = (dsn) => {
let input = rawInput[dsn]
const data = []
if (input && (input = input.split('\n'))) {
for (let line, iLine = 0, readings; (line = input[iLine++]) !== undefined;) {
if (line) {
if (/^---\sscanner\s\d+\s---$/i.exec(line)) {
data.push(readings = [])
} else {
readings.push(line.split(',').map(parseInt))
}
}
}
return { beacons: undefined, scanBlocks: data }
}
}
/** @typedef {[number,number,number]} TPoint */
/**
* @typedef {{
* diffs: TPoint[],
* [offset]: TPoint,
* pairs: [number, number][]
* [rotation]: number,
* scans: TPoint[]
* }} TBlock
*
* The diffs are 3D projections of distances between all possible point pairs.
* Matching those against diffs from another scanner is where the solution starts.
*/
/**
* @param {number} x
* @param {number} y
* @param {number} z
* @param {TPoint} pointB
* @return {TPoint} squared distances by all 3 projections
*/
const getDistances2 = ([x, y, z], pointB) => {
x -= pointB[0], y -= pointB[1], z -= pointB[2]
x *= x, y *= y, z *= z
return [y + z, x + z, x + y]
}
/**
* @param {[number, number, number][]} pointReadings
* @return {TBlock}
*/
const composeBlock = (pointReadings) => {
const diffs = [], pairs = [], scans = pointReadings.slice()
for (let i = 0; i < scans.length; ++i) {
for (let j = 0; j < i; ++j) {
diffs.push(getDistances2(pointReadings[i], pointReadings[j]))
pairs.push([j, i])
}
}
return { diffs, offset: [0, 0, 0], pairs, rotation: undefined, scans }
}
const doSetsMatch = (a, b) => {
let b1 = b.slice(0, 3), n = b1.length
for (let iA = 0, iB; iA < 3; ++iA) {
if ((iB = b1.indexOf(a[iA])) >= 0 && iB < 3) {
b1[iB] = undefined, --n
}
}
return n === 0
}
/**
* @param {number} value
* @param {number[]} pairs
* @param {number} start
*/
const indexesOfPairsContaining = (value, pairs, start) => {
const result = []
for (let i = start; (i = pairs.indexOf(value, i)) >= 0; ++i) {
result.push(i >> 1)
}
return result
}
/**
* @param {TBlock} blockA
* @param {TBlock} blockB
*/
const findSimilarPairs = (blockA, blockB) => {
const indexesA = [], indexesB = [], idxA = [], idxB = []
for (let i = 0, dA; (dA = blockA.diffs[i]); i += 1) {
for (let j = 0, dB; (dB = blockB.diffs[j]); j += 1) {
if (doSetsMatch(dA, dB)) {
indexesA.push(...blockA.pairs[i])
indexesB.push(...blockB.pairs[j])
}
}
}
for (let i = 0, indexA, indexB, a, b; (indexA = indexesA[i]) !== undefined; ++i) {
if (!idxA.includes(indexA)) {
if ((a = indexesOfPairsContaining(indexA, indexesA, i)).length > 1) {
b = undefined
if (!idxB.includes(indexB = indexesB[i])) {
if ((b = indexesOfPairsContaining(indexB, indexesB, 0)).length > 1) {
if (intersection(a, b).length > 1) {
idxA.push(indexA)
idxB.push(indexB)
}
}
}
if (!idxB.includes(indexB = indexesB[i ^ 1])) {
if ((b = indexesOfPairsContaining(indexB, indexesB, 0)).length > 1) {
if (intersection(a, b).length > 1) {
assert(!idxA.includes(indexA), 'inc')
idxA.push(indexA)
idxB.push(indexB)
}
}
}
}
}
}
if (idxA.length >= 12) {
const r = findRotation(blockA.scans[idxA[0]], blockA.scans[idxA[1]],
blockB.scans[idxB[0]], blockB.scans[idxB[1]])
assert(r !== undefined)
const points = blockB.scans.map(p => rotate(p, r))
const offs = getOffset(blockA.scans[idxA[0]], points[idxB[0]])
blockB.scans = points.map(p => shift(p, reverse(offs)))
blockB.rotation = r
blockB.offset = offs
return true
}
return false
}
/**
* @param {{beacons: TPoint[], blocks: TBlock[], scanBlocks:TPoint[][]}} input
*/
const solve1 = (input) => {
let was
/** @type {TBlock[]} */
const blocks = input.scanBlocks.map(composeBlock)
blocks[0].rotation = 0
do {
was = false
for (let i = 0; i < blocks.length; ++i) {
if (blocks[i].rotation !== undefined) {
for (let j = 0; j < blocks.length; ++j) {
if (j !== i && blocks[j].rotation === undefined) {
was |= findSimilarPairs(blocks[i], blocks[j])
}
}
}
}
} while (was)
const beacons = []
for (const block of blocks) {
for (const [x, y, z] of block.scans) {
if (!beacons.find(([a, b, c]) => a === x && b === y && c === z)) {
beacons.push([x, y, z])
}
}
}
input.beacons = beacons
input.blocks = blocks
return input
}
/**
* @param {{beacons: TPoint[], blocks: TBlock[], scanBlocks:TPoint[][]}} input
*/
const puzzle1 = (input) => {
return solve1(input).beacons.length
}
const manhattan = ([a, b, c], [d, e, f]) => abs(a - d) + abs(b - e) + abs(c - f)
/**
* @param {TBlock[]} blocks
*/
const puzzle2 = ({ blocks }) => {
let longest = 0
for (let i = 0; i < blocks.length; ++i) {
for (let j = 0; j < i; ++j) {
longest = max(longest, manhattan(blocks[i].offset, blocks[j].offset))
}
}
return longest
}
module.exports = { parse, puzzles: [puzzle1, puzzle2] }