Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 23, 2024
1 parent b27705a commit bac7302
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/2024/day23.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
function unique(networks) {
networks = networks.map(x => x.sort().join(","));
return [...new Set(networks)].map(x => x.split(","));
}

function addOneToNetworks(networks, map) {
let set = new Set();
networks.forEach(computers => {
networks = networks.flatMap(computers => {
let candidates = computers.map(c => map.get(c));
let result = candidates.reduce((a, b) => a.intersection(b));
result.forEach(x => set.add([...computers, x].sort().join(",")));
let result = [...candidates.reduce((a, b) => a.intersection(b))];
return result.map(x => [...computers, x]);
});
return set;
return unique(networks);
}

function parse(input) {
Expand All @@ -20,15 +24,13 @@ function parse(input) {

export function part1(input) {
let { networks, map } = parse(input);
let set = addOneToNetworks(networks, map);
return [...set].filter(x => x.match(/(^t|,t)/)).length;
networks = addOneToNetworks(networks, map);
networks = networks.filter(c => c.some(x => x.startsWith("t")));
return networks.length;
}

export function part2(input) {
let { networks, map } = parse(input);
while (networks.length > 1) {
let next = addOneToNetworks(networks, map);
networks = [...next].map(x => x.split(","));
}
while (networks.length > 1) networks = addOneToNetworks(networks, map);
return networks[0].sort().join(",");
}

0 comments on commit bac7302

Please sign in to comment.