adventofcode-2018/07/part1.js

17 lines
635 B
JavaScript
Raw Permalink Normal View History

2018-12-08 20:35:38 +00:00
var input = require("fs").readFileSync("input.txt").toString().split("\n").filter(a=>a);
var moves = input.map(line=>(line.split(" "))).map(a=>([a[1], a[7]]));
var statesObj = {};
for(var state of moves.map(a=>(a[0])).concat(moves.map(a=>(a[1])))){
statesObj[state] = 0;
}
var states = Object.keys(statesObj);
var visited = [];
while(visited.length < states.length){
var possibleMoves = states.filter(s=>(moves.filter(m=>(m[1]==s)).map(m=>(m[0])).filter(ms=>(visited.indexOf(ms) == -1)).length == 0)).filter(s=>(visited.indexOf(s) == -1)).sort();
var move = possibleMoves[0];
visited.push(move);
}
console.log(visited.join(""));