2026-04-06 20:15:21 +0300 MSK
Walking Robot Simulation
Links
Code
class Solution:
def __init__(self):
self.HASH_MULTIPLIER = (
60013 # Slightly larger than 2 * max coordinate value
)
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
# Store obstacles in an set for efficient lookup
obstacle_set = {self._hash_coordinates(x, y) for x, y in obstacles}
# Define direction vectors: North, East, South, West
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
x, y = 0, 0
max_distance_squared = 0
current_direction = 0 # 0: North, 1: East, 2: South, 3: West
for command in commands:
if command == -1: # Turn right
current_direction = (current_direction + 1) % 4
continue
if command == -2: # Turn left
current_direction = (current_direction + 3) % 4
continue
# Move forward
dx, dy = directions[current_direction]
for _ in range(command):
next_x, next_y = x + dx, y + dy
if self._hash_coordinates(next_x, next_y) in obstacle_set:
break
x, y = next_x, next_y
max_distance_squared = max(max_distance_squared, x * x + y * y)
return max_distance_squared
# Hash function to convert (x, y) coordinates to a unique integer value
def _hash_coordinates(self, x: int, y: int) -> int:
return x + self.HASH_MULTIPLIER * y