diff --git a/day17/part1.js b/day17/part1.js new file mode 100644 index 0000000..ecc328d --- /dev/null +++ b/day17/part1.js @@ -0,0 +1,56 @@ +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("")); diff --git a/day17/part2.js b/day17/part2.js new file mode 100644 index 0000000..e4f38d4 --- /dev/null +++ b/day17/part2.js @@ -0,0 +1,61 @@ +const input = "edjrjqaa"; + +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; +} + +//console.log(getCoords("DDUDRUULRRRDDLRUDULDRLDLUDURLULRLRUDRUDDUDULLDRUDLRLUDDURRLUDRLRLURDUDLUDLRUUDDDRLUUDRLLUURLDDRRRLURUDLLDDLUDRUDRUDLUUDRULRLUDRULDRLRLDLRUDRLURDDLUUDULRRRLLRLDRDLRLRURLLDUDUUUDLURLDURRDDUDULDLRUUDDLUDDRUUURRDDLRLLRLUDURDRULLRUDLDDUDLURRUUDULRDLDLRUULRRDRULDDUDLULRRDULLURDURDDUDUUDUDUDDDLRLUDUDURLLDUURRDRUDULLLRDDURRLUDURLLRRULLRRLLDRLRURDUDLLDRLURRLUDLDRURLLRURLLLDRLRRUDDLUDRUDLLUDDURRUUDLUDUDLRRDLUULDRDDRUDULRUDRLUDRLDLLRULRULDUDRLRURLRLDLRDULURDRURLDUURLRLRLDRLDLURDULRUDRLRUDDLDLRLUDRR")); + +var paths = [""]; +while(paths.filter((path)=>(getCoords(path).filter((i)=>(i == 3)).length != 2)).length > 0){ + paths = paths.filter((path)=>(getCoords(path).filter((i)=>(i == 3)).length != 2)); + 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)).map((str)=>(str.length))); +} + +console.log(paths.filter((path)=>(getCoords(path).filter((i)=>(i == 3)).length != 2))[0].length); + +//console.log(paths.filter((path)=>(getCoords(path).filter((i)=>(i == 3)).length == 2))[0]);