def solution(dirs):
visit = set()
x = 0; y = 0
for d in dirs:
if d == 'U' and y < 5:
visit.add(((x, y), (x, y+1)))
y += 1
elif d == 'D' and y > -5:
visit.add(((x, y-1), (x, y)))
y -= 1
elif d == 'R' and x < 5:
visit.add(((x, y), (x+1, y)))
x += 1
elif d == 'L' and x > -5:
visit.add(((x-1, y), (x, y)))
x -= 1
return len(visit)
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[0][i]
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