const visited = new Set();
const arrowDict = {
// x, y
U: [ 0, 1],
D: [ 0,-1],
R: [ 1, 0],
L: [-1, 0],
}
function token(x, y){
return String(x)+String(y);
}
function normalize(start, end){
if(start > end){
return start+end;
}
return end+start;
}
function solution(dirs) {
let now = [0,0]
for (const dir of dirs){
const arrow = arrowDict[dir];
const nx = now[0] + arrow[0];
const ny = now[1] + arrow[1];
if(-5 <= nx && nx <= 5 && -5 <= ny && ny <= 5){
const start = token(now[0], now[1]);
const end = token(nx, ny);
const road = normalize(start, end)
visited.add(road);
now = [nx, ny]
}
}
return visited.size;
}