|
| 1 | +function straightLineDistance3D(a, b) { |
| 2 | + return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2); |
| 3 | +} |
| 4 | + |
| 5 | +function top3Circuits(junctions) { |
| 6 | + let circuitCounts = new Map(); |
| 7 | + for (let junction of junctions) { |
| 8 | + if (junction.circuit === null) continue; |
| 9 | + circuitCounts.set( |
| 10 | + junction.circuit, |
| 11 | + (circuitCounts.get(junction.circuit) || 0) + 1, |
| 12 | + ); |
| 13 | + } |
| 14 | + let counts = Array.from(circuitCounts.values()); |
| 15 | + counts.sort((a, b) => b - a); |
| 16 | + return counts.slice(0, 3).reduce((a, b) => a * b, 1); |
| 17 | +} |
| 18 | + |
| 19 | +function getAllPairs(junctions) { |
| 20 | + let pairs = []; |
| 21 | + for (let i = 0; i < junctions.length; i++) { |
| 22 | + for (let j = i + 1; j < junctions.length; j++) { |
| 23 | + let a = junctions[i]; |
| 24 | + let b = junctions[j]; |
| 25 | + let dist = straightLineDistance3D(a, b); |
| 26 | + pairs.push({ a, b, dist }); |
| 27 | + } |
| 28 | + } |
| 29 | + return pairs.sort((p1, p2) => p1.dist - p2.dist); |
| 30 | +} |
| 31 | + |
| 32 | +function connectPair(pair, junctions) { |
| 33 | + let { a, b } = pair; |
| 34 | + if (a.circuit === null && b.circuit === null) { |
| 35 | + a.circuit = b.circuit = Symbol(); |
| 36 | + } else if (a.circuit !== null && b.circuit === null) { |
| 37 | + b.circuit = a.circuit; |
| 38 | + } else if (a.circuit === null && b.circuit !== null) { |
| 39 | + a.circuit = b.circuit; |
| 40 | + } else if (a.circuit !== b.circuit) { |
| 41 | + let oldCircuit = b.circuit; |
| 42 | + let newCircuit = a.circuit; |
| 43 | + for (let j = 0; j < junctions.length; j++) { |
| 44 | + if (junctions[j].circuit === oldCircuit) { |
| 45 | + junctions[j].circuit = newCircuit; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return pair; |
| 50 | +} |
| 51 | + |
| 52 | +function allConnected(junctions) { |
| 53 | + let firstCircuit = junctions[0].circuit; |
| 54 | + if (firstCircuit === null) return false; |
| 55 | + for (let junction of junctions) { |
| 56 | + if (junction.circuit !== firstCircuit) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +export function part1(input, times = 1000) { |
| 64 | + let junctions = input.split("\n").map(line => { |
| 65 | + let [x, y, z] = line.split(",").map(Number); |
| 66 | + return { x, y, z, circuit: null }; |
| 67 | + }); |
| 68 | + let pairs = getAllPairs(junctions).slice(0, times); |
| 69 | + for (let i = 0; i < pairs.length; i++) connectPair(pairs[i], junctions); |
| 70 | + return top3Circuits(junctions); |
| 71 | +} |
| 72 | + |
| 73 | +export function part2(input) { |
| 74 | + let junctions = input.split("\n").map(line => { |
| 75 | + let [x, y, z] = line.split(",").map(Number); |
| 76 | + return { x, y, z, circuit: null }; |
| 77 | + }); |
| 78 | + let pair; |
| 79 | + let pairs = getAllPairs(junctions); |
| 80 | + while (!allConnected(junctions)) pair = connectPair(pairs.shift(), junctions); |
| 81 | + return pair.a.x * pair.b.x; |
| 82 | +} |
0 commit comments