> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Walking Robot Simulation Python Solution

> Tested Python solution for LeetCode 874 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 874, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/walking-robot-simulation/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 874   # by problem number
lcpy gen -s walking_robot_simulation   # by problem name
```

## Problem

A robot on an infinite XY-plane starts at point \<code>(0, 0)\</code> facing north. The robot receives an array of integers \<code>commands\</code>, which represents a sequence of moves that it needs to execute. There are only three possible types of instructions the robot can receive:\</p>

\<ul>
\<li>\<code>-2\</code>: Turn left 90 degrees.\</li>
\<li>\<code>-1\</code>: Turn right 90 degrees.\</li>
\<li>\<code>1 \<= k \<= 9\</code>: Move forward \<code>k\</code> units, one unit at a time.\</li>
\</ul>

\<p>Some of the grid squares are obstacles. The \<code>ith\</code> obstacle is at grid point \<code>obstacles\[i] = (xi, yi)\</code>. If the robot runs into an obstacle, it will stay in its current location (on the block adjacent to the obstacle) and move onto the next command.\</p>

\<p>Return \<em>the maximum squared Euclidean distance that the robot reaches at any point in its path\</em>.\</p>

### Examples

```
Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation: The robot starts at (0, 0):
1. Move north 4 units to (0, 4).
2. Turn right to face east.
3. Move east 3 units to (3, 4).
The furthest point the robot ever gets from the origin is (3, 4), which squared is 3^2 + 4^2 = 25 units away.
```

```
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
Explanation: The robot is being tracked:
1. Move north 4 units to (0, 4).
2. Turn right to face east.
3. Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4).
4. Turn left to face north.
5. Move north 4 units to (1, 8).
The furthest point the robot ever gets from the origin is (1, 8), which squared is 1^2 + 8^2 = 65 units away.
```

```
Input: commands = [6,-1,-1,6], obstacles = []
Output: 36
Explanation: The robot starts at (0, 0):
1. Move north 6 units to (0, 6).
2. Turn right to face east.
3. Turn right to face south.
4. Move south 6 units to (0, 0).
The furthest point the robot ever gets from the origin is (0, 6), which squared is 6^2 = 36 units away.
```

### Constraints

* 1 \<= commands.length \<= 10^4
* commands\[i] is either -2, -1, or an integer in the range \[1, 9].
* 0 \<= obstacles.length \<= 10^4
* -3 \* 10^4 \<= xi, yi \<= 3 \* 10^4
* The answer is guaranteed to be less than 2^31.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/walking_robot_simulation/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/walking_robot_simulation/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(c + o)
    # Space: O(o)
    def robot_sim(self, commands: list[int], obstacles: list[list[int]]) -> int:
        blocked = {(x, y) for x, y in obstacles}
        x, y, dx, dy = 0, 0, 0, 1
        best = 0
        for command in commands:
            if command == -2:
                dx, dy = -dy, dx
            elif command == -1:
                dx, dy = dy, -dx
            else:
                for _ in range(command):
                    next_x, next_y = x + dx, y + dy
                    if (next_x, next_y) in blocked:
                        break
                    x, y = next_x, next_y
                best = max(best, x * x + y * y)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(c + o) | O(o) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.