adventofcode-2016/day02/part1.js

28 lines
603 B
JavaScript
Raw Normal View History

2016-12-02 13:03:11 +00:00
var fs = require("fs");
var input = fs.readFileSync("input.txt").toString().replace(/\r/g, "");
var moves = {
U: -3,
D: 3,
L: -1,
R: 1
};
var prev = 5;
var locations = input.split("\n").slice(0, -1).map(function(curr, i, arr){
return curr.split("").reduce(function(pos, char){
var newpos = pos + moves[char];
if(Math.abs(moves[char]) == 1){
if(Math.abs((newpos - 1)%3 - (pos - 1)%3) == 1){
prev = newpos;
}
}
else{
if(newpos >= 1 && newpos <= 9){
prev = newpos;
}
}
return prev;
}, prev);
});
console.log(locations.join(""));