42 lines
974 B
JavaScript
42 lines
974 B
JavaScript
|
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
|
||
|
var dirs = input.split(",");
|
||
|
function simplify(dirs){
|
||
|
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]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return dirs;
|
||
|
}
|
||
|
|
||
|
var maxlen = 0;
|
||
|
|
||
|
for(var i = 0; i < dirs.length; i++){
|
||
|
var sub = dirs.slice(0, i);
|
||
|
var len = simplify(sub).length;
|
||
|
if(len > maxlen){
|
||
|
maxlen = len;
|
||
|
}
|
||
|
console.log(i/dirs.length);
|
||
|
}
|
||
|
console.log(maxlen);
|