adventofcode-2016/day17/part1.js

57 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2016-12-17 12:29:32 +00:00
const input = "kglvqrro";
var moveTypes = {
U: [0, -1],
D: [0, 1],
L: [-1, 0],
R: [1, 0],
}
md5 = (str)=>{
var md5sum = require("crypto").createHash("md5");
md5sum.update(str);
return md5sum.digest("hex");
}
doorOpen = (path, pos)=>(Boolean(md5(input + path)[pos].match(/[bcdef]/)));
getCoords = (path)=>(path.split("").map((dir)=>(moveTypes[dir]))).reduce((coord, displacement)=>(coord.map((a, i)=>(a + displacement[i]))), [0, 0]);
getNextPath = (path)=>{
[x, y] = getCoords(path);
var paths = [];
if(y > 0){
if(doorOpen(path, 0)){
paths.push(path + "U");
}
}
if(y < 3){
if(doorOpen(path, 1)){
paths.push(path + "D");
}
}
if(x > 0){
if(doorOpen(path, 2)){
paths.push(path + "L");
}
}
if(x < 3){
if(doorOpen(path, 3)){
paths.push(path + "R");
}
}
return paths;
}
var paths = [""];
while(paths.map(getCoords).filter(([x, y])=>((x == 3) && (y == 3))).length == 0){
var newpaths = []
for(var path of paths){
newpaths = newpaths.concat(getNextPath(path));
}
paths = newpaths;
}
console.log(paths.filter((path)=>(getCoords(path).filter((i)=>(i == 3)).length == 2))[0]);
//console.log(getNextPath(""));