Skip to content

Commit 0de27c6

Browse files
committed
refactor
1 parent 82bbeac commit 0de27c6

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

src/2025/day08.js

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,63 @@
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-
51
function top3Circuits(junctions) {
62
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);
136
}
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);
168
return counts.slice(0, 3).reduce((a, b) => a * b, 1);
179
}
1810

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-
3211
function connectPair(pair, junctions) {
3312
let { a, b } = pair;
3413
if (a.circuit === null && b.circuit === null) {
3514
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;
4017
} 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));
4821
}
4922
return pair;
5023
}
5124

5225
function allConnected(junctions) {
5326
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+
);
6131
}
6232

63-
export function part1(input, times = 1000) {
33+
function parse(input) {
6434
let junctions = input.split("\n").map(line => {
6535
let [x, y, z] = line.split(",").map(Number);
6636
return { x, y, z, circuit: null };
6737
});
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);
7055
return top3Circuits(junctions);
7156
}
7257

7358
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-
});
7859
let pair;
79-
let pairs = getAllPairs(junctions);
60+
let { junctions, pairs } = parse(input);
8061
while (!allConnected(junctions)) pair = connectPair(pairs.shift(), junctions);
8162
return pair.a.x * pair.b.x;
8263
}

0 commit comments

Comments
 (0)