Skip to content

Commit d57c755

Browse files
committed
refactor
1 parent 7730046 commit d57c755

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/2025/day08.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
function circuitSizes(junctions) {
2-
let circuitCounts = new Map();
3-
for (let { circuit } of junctions) {
4-
if (circuit === null) continue;
5-
circuitCounts.set(circuit, (circuitCounts.get(circuit) || 0) + 1);
6-
}
7-
return Array.from(circuitCounts.values()).sort((a, b) => b - a);
2+
let connected = junctions.filter(junction => junction.circuit !== null);
3+
let circuits = Map.groupBy(connected, junction => junction.circuit);
4+
let sizes = circuits.values().map(circuit => circuit.length);
5+
return sizes.toArray().sort((a, b) => b - a);
86
}
97

108
function connectPair(pair, junctions) {
@@ -22,11 +20,8 @@ function connectPair(pair, junctions) {
2220
}
2321

2422
function allConnected(junctions) {
25-
let firstCircuit = junctions[0].circuit;
26-
return (
27-
firstCircuit !== null &&
28-
junctions.every(junction => junction.circuit === firstCircuit)
29-
);
23+
let { circuit } = junctions[0];
24+
return circuit && junctions.every(junction => junction.circuit === circuit);
3025
}
3126

3227
function parse(input) {

0 commit comments

Comments
 (0)