visited = set() hx, hy = 0, 0 tx, ty = 0, 0 for m in motions: dir, step = m step = int(step) for i inrange(step): hx = hx + move[dir][0] hy = hy + move[dir][1] dx = hx - tx dy = hy - ty ifabs(dx) >= 2orabs(dy) >= 2: tx += int(dx>0) - int(dx<0) ty += int(dy>0) - int(dy<0) visited.add((tx, ty)) print(len(visited))