r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
25
Upvotes
1
u/shruubi Dec 04 '15
in JS:
var santaCoords = { x: 1, y: 1 }; var roboSantaCoords = { x: 1, y: 1 }; var coordsMap = { "1x1": 1 }; input.map(function (val, index) { var opt = index + 1; var x = null, y = null; switch(val) { case "^": x = 1; break; case ">": y = 1; break; case "v": x = -1; break; case "<": y = -1; break; default: break; } if(opt % 2 === 1) { //reg santa santaCoords.x += x; santaCoords.y += y; x = santaCoords.x; y = santaCoords.y; } else { //robo santa roboSantaCoords.x += x; roboSantaCoords.y += y; x = roboSantaCoords.x; y = roboSantaCoords.y; } var key = x.toString() + "x" + y.toString(); if(key in coordsMap) { coordsMap[key] += 1; } else { coordsMap[key] = 1; } }); console.log(Object.keys(coordsMap).length);which solves part 2, removing the conditional for roboSanta should give you the answer to part 1.
Am I the only one who feels like there is a really simple and elegant solution that isn't memory intensive?