From bac7302a7b79979ebd03ddae4dcaeb3b18ff405e Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Mon, 23 Dec 2024 11:31:32 +0200 Subject: [PATCH] refactor --- src/2024/day23.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/2024/day23.js b/src/2024/day23.js index 5335eb14..8d83ab03 100644 --- a/src/2024/day23.js +++ b/src/2024/day23.js @@ -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) { @@ -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(","); }