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