Leetcode 906 Solution

This article provides solution to leetcode question 906 (walking-robot-simulation)

https://leetcode.com/problems/walking-robot-simulation

Solution

class Solution(object): def robotSim(self, commands, obstacles): """ :type commands: List[int] :type obstacles: List[List[int]] :rtype: int """ dirs = [ (0, 1), (-1, 0), (0, -1), (1, 0), ]
obstaclesSet = set(map(tuple, obstacles))
dir_index = 0 x, y = 0, 0 ans = 0
for cmd in commands: if cmd == -2: dir_index = (dir_index + 1) % 4 elif cmd == -1: dir_index = (dir_index + 3) % 4 else: for k in xrange(cmd): dx = dirs[dir_index][0] dy = dirs[dir_index][1]
if (x + dx, y + dy) in obstaclesSet: break
x += dx y += dy
ans = max(x * x + y * y, ans)
return ans