Added day 17
This commit is contained in:
parent
f8cf05425a
commit
0bd0b89e91
56
day17/part1.js
Normal file
56
day17/part1.js
Normal file
@ -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(""));
|
61
day17/part2.js
Normal file
61
day17/part2.js
Normal file
@ -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]);
|
Loading…
x
Reference in New Issue
Block a user