https://school.programmers.co.kr/learn/courses/30/lessons/49994
from collections import defaultdict
def solution(dirs):
visited = defaultdict(int)
#상하좌우 지정
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
answer = 0
cur_x, cur_y = 0, 0
for d in list(dirs):
next_x, next_y = 0, 0
if d == 'U':
next_x, next_y = cur_x + dx[0], cur_y + dy[0]
elif d == 'D':
next_x, next_y = cur_x + dx[1], cur_y + dy[1]
elif d == 'L':
next_x, next_y = cur_x + dx[2], cur_y + dy[2]
elif d == 'R':
next_x, next_y = cur_x + dx[3], cur_y + dy[3]
if -5 <= next_x <= 5 and -5 <= next_y <= 5:
temp = [(cur_x, cur_y), (next_x, next_y)]
temp.sort()
temp = [''.join(str(t)) for t in temp]
road_id = ''.join(temp)
if not visited[road_id]:
visited[road_id] += 1
answer += 1
cur_x, cur_y = next_x, next_y
return answer
def solution(dirs):
s = set()
d = {'U': (0,1), 'D': (0, -1), 'R': (1, 0), 'L': (-1, 0)}
x, y = 0, 0
for i in dirs:
nx, ny = x + d[i][0], y + d[i][1]
if -5 <= nx <= 5 and -5 <= ny <= 5:
s.add((x,y,nx,ny))
s.add((nx,ny,x,y))
x, y = nx, ny
return len(s)//2