-
Notifications
You must be signed in to change notification settings - Fork 0
/
walking_robot_simulation.py
32 lines (26 loc) · 1.03 KB
/
walking_robot_simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import List
class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
latitude, longitude = 0, 0
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
curr_dir = 0
obstacles = set(tuple(obstacle) for obstacle in obstacles)
furthest = 0
for command in commands:
if command > 0:
# Move robot but check for obstacles
x, y = directions[curr_dir]
for i in range(command):
new_x, new_y = latitude + x, longitude + y
if (new_x, new_y) in obstacles:
break
furthest = max(furthest, new_x ** 2 + new_y ** 2)
latitude, longitude = new_x, new_y
elif command == -2:
curr_dir = (curr_dir + 3) % 4
else:
curr_dir = (curr_dir + 1) % 4
return furthest
# commands = [6, -1, -1, 6]
# obstacles = []
# print(Solution().robotSim(commands, obstacles))