28 lines
603 B
JavaScript
28 lines
603 B
JavaScript
|
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(""));
|