Added day 13 part 2

This commit is contained in:
Tim Stallard 2016-12-13 23:09:07 +00:00
parent ab35e7136e
commit d9d37754fd

43
day13/part2.js Normal file
View File

@ -0,0 +1,43 @@
var input = 1352;
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(i < 50){
coords = getNextAllCoords();
i++;
}
console.log("Locations visited: ", previous.length);