adventofcode-2016/day01/part2.js

26 lines
775 B
JavaScript

var moves = [[1, 0], [0, -1], [-1, 0], [0, 1]];
var coords = [0, 0];
var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "").replace(/\n/g, "");
var steps = input.replace(/L/g, "R0, R0, R").split(", ").map((a)=>parseInt(a.substr(1)));
var locations = [];
function addCoord(x, y){
if(!locations[x]){
locations[x] = [];
}
if(!locations[x][y]){
locations[x][y] = 0;
}
locations[x][y]++;
}
var coords = [0, 0]
for(var step in steps){
for(var i = 0; i < steps[step]; i++){
coords = [coords[0] + moves[step%4][0], coords[1] + moves[step%4][1]];
addCoord(coords[0], coords[1]);
if(locations[coords[0]][coords[1]] == 2){
console.log(coords);
console.log(coords.map(Math.abs).reduce((a, b)=>(a + b)));
}
}
}