Walking Robot Simulation

Problem Id: 874 Difficulty: Easy


Intuition

Solution


class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        obstacles = set(map(tuple, obstacles))
        x = y = 0
        direc = [0, 1]
        ans = 0
        for c in commands:
            if c == -2:
                direc = [-direc[1], direc[0]]
            elif c == -1:
                direc = [direc[1], -direc[0]]
            else:
                while c > 0:
                    if (x + direc[0], y + direc[1]) not in obstacles:
                        x += direc[0]
                        y += direc[1]
                        c -= 1
                    else:
                        break
            ans = max(ans, x * x + y * y)
        return ans