const fs = require('fs'); const input = fs.readFileSync(__dirname + '/input.txt', 'utf8'); const connections: { [key: string]: Array }= {}; input.split("\n").forEach((conn: string) => { const [c1, c2] = conn.split('-'); connections[c1] = [c2, ...(connections[c1] || [])] connections[c2] = [c1, ...(connections[c2] || [])] }); let nets: Set = new Set; for (let key in connections) { const conns = connections[key]; conns.forEach((c2) => { connections[c2].forEach((c3) => { if (connections[c3].includes(key)) { const arr = [key, c2, c3]; arr.sort(); nets.add(arr.join(',')); } }) }); } while (nets.size > 1) { const nextNets: Set = new Set; for (let net of nets) { const cps = net.split(','); for (let i in connections) { if (cps.includes(i)) { continue; } const conns = connections[i]; if (cps.every((id) => conns.includes(id))) { const newNet = [...cps, i]; newNet.sort(); nextNets.add(newNet.join(',')); } } } nets = nextNets; } console.log(nets);