adventofcode-2016/day13/part1.js

47 lines
1.1 KiB
JavaScript

var input = 1352;
[targetx, targety] = [31, 39];
toBin = (number)=>(Number(number).toString(2).split("").map((a)=>(parseInt(a))));
evenSum = (parts)=>(parts.reduce((a, b)=>(a+b))%2 == 0);
checkCoord = (x, y)=>(evenSum(toBin(((x*x + 3*x + 2*x*y + y + y*y) + input))));
var previous = [];
function getNextCoords(x, y){
var next = [];
if(x > 0){
if(checkCoord(x-1, y)){
next.push([x-1, y]);
}
}
if(y > 0){
if(checkCoord(x, y-1)){
next.push([x, y-1]);
}
}
if(checkCoord(x+1, y)){
next.push([x+1, y]);
}
if(checkCoord(x, y+1)){
next.push([x, y+1]);
}
next = next.filter((pair)=>(previous.indexOf(pair.join(",")) == -1));
next.forEach((pair)=>{previous.push(pair.join(","))});
return next;
}
var coords = [[1,1]];
function getNextAllCoords(){
return coords.map((pair)=>(getNextCoords(pair[0], pair[1]))).reduce((a, b)=>(a.concat(b)), []);
}
var i = 0;
while(coords.filter(([x,y])=>((x == targetx) && (y == targety))).length == 0){
coords = getNextAllCoords();
i++;
console.log(i);
}
console.log("Steps: ", i);