var board = [ {"0": 1}, {"-1": 2, "0": 3, "1": 4}, {"-2": 5, "-1": 6, "0": 7, "1": 8, "2": 9}, {"-1": "A", "0": "B", "1": "C"}, {"0": "D"} ]; var moves = { U: [-1, 0], D: [1, 0], L: [0, -1], R: [0, 1], } var pos = [2, -2]; var locations = []; var fs = require("fs"); var input = fs.readFileSync("input.txt").toString().replace(/\r/g, ""); input.split("\n").slice(0, -1).forEach(function(line){ if(line){ line.split("").forEach(function(char){ if(char){ var move = moves[char]; var newpos = pos.map((a, x)=>(a + move[x])); if(board[newpos[0]]){ if(board[newpos[0]][newpos[1]]){ pos = newpos; } } } }); } console.log(board[pos[0]][pos[1]]); locations.push(board[pos[0]][pos[1]]); }); console.log(locations.join(""));