https://school.programmers.co.kr/learn/courses/30/lessons/49994
func solution(_ dirs:String) -> Int {
var now = [0, 0]
var visited: Set<[Int]> = []
for i in dirs {
var (dx, dy) = (0,0)
switch i {
case "U": (dx, dy) = (0, 1)
case "D": (dx, dy) = (0, -1)
case "L": (dx, dy) = (-1, 0)
case "R": (dx, dy) = (1, 0)
default: break
}
let next = [now[0]+dx, now[1]+dy]
if abs(next[0]) > 5 || abs(next[1]) > 5 {
continue
}
// 중요 !!
//(0,0)에서(0,1)로 간 길이나 (0,1)에서(0,0)로 간 길은 같기 때문에 둘다 체크 필요!
if !visited.contains(now+next) && !visited.contains(next+now){
visited.insert(now+next)
}
now = next
}
return visited.count
}