diff --git a/day13/part2.js b/day13/part2.js new file mode 100644 index 0000000..36ad83d --- /dev/null +++ b/day13/part2.js @@ -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);