adventofcode-2017/day11/part1.js

31 lines
730 B
JavaScript
Raw Permalink Normal View History

2017-12-16 12:12:56 +00:00
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var dirs = input.split(",");
var replacements = [
[["n", "s"], ""],
[["ne", "sw"], ""],
[["nw", "se"], ""],
[["ne", "nw"], "n"],
[["se", "sw"], "s"],
[["ne", "s"], "se"],
[["se", "n"], "ne"],
[["sw", "n"], "nw"],
[["nw", "s"], "sw"],
];
var oldlength = 0;
while(dirs.length != oldlength){
oldlength = dirs.length;
for(var replacement of replacements){
if(dirs.indexOf(replacement[0][0]) > -1 && dirs.indexOf(replacement[0][1]) > -1){
dirs.splice(dirs.indexOf(replacement[0][0]), 1);
dirs.splice(dirs.indexOf(replacement[0][1]), 1);
if(replacement[1]){
dirs.push(replacement[1]);
}
}
}
}
console.log(dirs.length);