Skip to content

Commit 6aa5a94

Browse files
committed
refactor
1 parent 3f321d5 commit 6aa5a94

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/2024/day23.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
1-
function addOneToNetwork(network, connections = network) {
1+
function addOneToNetworks(networks, connections = networks) {
22
let set = new Set();
3-
network.forEach(connection => {
4-
let as = connection
3+
networks.forEach(computers => {
4+
let candidates = computers
55
.map(c => {
66
return connections
7-
.filter(other => other.includes(c))
8-
.map(a => a.find(x => x !== c));
7+
.filter(pair => pair.includes(c))
8+
.map(pair => pair.find(x => x !== c));
99
})
10-
.map(a => new Set(a));
11-
let result = as.reduce((a, b) => a.intersection(b));
12-
result.forEach(x => set.add([...connection, x].sort().join(",")));
10+
.map(x => new Set(x));
11+
let result = candidates.reduce((a, b) => a.intersection(b));
12+
result.forEach(x => set.add([...computers, x].sort().join(",")));
1313
});
1414
return set;
1515
}
1616

1717
export function part1(input) {
18-
let connections = input.split("\n").map(line => {
19-
let [a, b] = line.split("-").sort();
20-
return [a, b];
21-
});
22-
let set = addOneToNetwork(connections);
18+
let connections = input.split("\n").map(line => line.split("-"));
19+
let set = addOneToNetworks(connections);
2320
return [...set].filter(x => x.match(/(^t|,t)/)).length;
2421
}
2522

2623
export function part2(input) {
27-
let connections = input.split("\n").map(line => {
28-
let [a, b] = line.split("-").sort();
29-
return [a, b];
30-
});
31-
let network = connections;
32-
while (network.length > 1) {
33-
let next = addOneToNetwork(network, connections);
34-
network = [...next].map(x => x.split(","));
24+
let connections = input.split("\n").map(line => line.split("-"));
25+
let networks = connections;
26+
while (networks.length > 1) {
27+
let next = addOneToNetworks(networks, connections);
28+
networks = [...next].map(x => x.split(","));
3529
}
36-
return network[0].sort().join(",");
30+
return networks[0].sort().join(",");
3731
}

0 commit comments

Comments
 (0)