From d9d37754fd80c9f70123dfb4fd0ff9fb8af96971 Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Tue, 13 Dec 2016 23:09:07 +0000 Subject: [PATCH] Added day 13 part 2 --- day13/part2.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 day13/part2.js 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);