20 lines
651 B
JavaScript
20 lines
651 B
JavaScript
|
var input = require("fs").readFileSync("input.txt").toString();
|
||
|
var links = input.split("\n").filter((a)=>(a)).map((a)=>(a.split(" <-> ")[1].split(", ").map((b)=>(parseInt(b)))));
|
||
|
var allvisited = [];
|
||
|
function getconnected(x, items){
|
||
|
if(items.indexOf(x) == -1){
|
||
|
items.push(x);
|
||
|
links[x].forEach((a)=>(getconnected(a, items)));
|
||
|
}
|
||
|
return items;
|
||
|
}
|
||
|
var i = 0;
|
||
|
while(allvisited.length != Object.keys(links).length){
|
||
|
var start = Object.keys(links).map((a)=>(parseInt(a))).filter((a)=>(allvisited.indexOf(a) == -1))[0];
|
||
|
console.log(i, start);
|
||
|
var newgroup = getconnected(start, []);
|
||
|
allvisited = allvisited.concat(newgroup);
|
||
|
i++
|
||
|
}
|
||
|
console.log(i);
|