07.29에 푼 문제입니다🌷
방문 길이
function solution(dirs) {
var answer = 0;
const point=[0,0]
const points= []
for(let dir of dirs){
let pointstr1 =String(point[0])+String(point[1])
if(dir==='U'){
if(point[1]+1>5) continue
point[1]++
checkpoints(pointstr1)
}
else if(dir==='D'){
if(point[1]-1<-5) continue
point[1]--
checkpoints(pointstr1)
}
else if(dir==='R'){
if(point[0]+1>5) continue
point[0]++
checkpoints(pointstr1)
}
else if(dir==='L'){
if(point[0]-1<-5) continue
point[0]--
checkpoints(pointstr1)
}
}
function checkpoints(pointstr1){
let pointstr2=String(point[0])+String(point[1])
if(!(points.includes(pointstr1+pointstr2)
||points.includes(pointstr2+pointstr1))){
answer++
points.push(pointstr1+pointstr2)
}
}
return answer;
}