adventofcode-2016/day01/part1.js

7 lines
436 B
JavaScript

var moves = [[1, 0], [0, -1], [-1, 0], [0, 1]];
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 finalCoords = steps.map((a, i)=>moves[i%4].map((b)=>(b*a))).reduce((a, b)=>a.map((x, i)=>(x + b[i])));
var distance = finalCoords.map(Math.abs).reduce((a, b)=>(a + b));
console.log(distance);