From ab35e7136e61dca3bd155ab67846d1ff430170a6 Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Tue, 13 Dec 2016 18:23:57 +0000 Subject: [PATCH] Added day 13, part 1 only --- day13/part1.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 day13/part1.js diff --git a/day13/part1.js b/day13/part1.js new file mode 100644 index 0000000..2f8ba74 --- /dev/null +++ b/day13/part1.js @@ -0,0 +1,46 @@ +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);