48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
const fs = require('fs');
|
|
|
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
|
|
|
const connections: { [key: string]: Array<string> }= {};
|
|
|
|
input.split("\n").forEach((conn: string) => {
|
|
const [c1, c2] = conn.split('-');
|
|
connections[c1] = [c2, ...(connections[c1] || [])]
|
|
connections[c2] = [c1, ...(connections[c2] || [])]
|
|
});
|
|
|
|
let nets: Set<string> = 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<string> = 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); |