function solution(park, routes) {
const H = park.length;
const W = park[0].length;
const dirs = {
N: [-1, 0],
S: [1, 0],
W: [0, -1],
E: [0, 1],
};
let x, y;
for (let i = 0; i < H; i++) {
for (let j = 0; j < W; j++) {
if (park[i][j] === "S") {
x = i;
y = j;
}
}
}
for (const route of routes) {
const [op, n] = route.split(" ");
const N = Number(n);
let flag = true;
let nx = x;
let ny = y;
for (let i = 0; i < N; i++) {
nx += dirs[op][0];
ny += dirs[op][1];
if (nx < 0 || nx >= H || ny < 0 || ny >= W) {
flag = false;
break;
}
if (park[nx][ny] === "X") {
flag = false;
break;
}
}
if (flag) {
x = nx;
y = ny;
}
}
return [x, y]
}