KatInterview

Simulate Grid Rover Fleet Collision Logic

Arrays & HashingModel answer
MediumModel answerArrays & Hashingarrayssimulationmatrix

Last updated

Simulate the concurrent step-by-step navigation of multiple automated grid rovers, handling collisions, static obstacles, and grid bounds.

You are building the simulation module for an automated logistics center. The center operates on a 2D Cartesian grid of dimension width × height. Multiple autonomous vehicles (rovers) navigate this grid according to individual instruction strings.

Write a function simulate_rovers(grid_size, rovers, commands, obstacles) that computes and returns the final (x, y) coordinates of all rovers after all instructions have been processed.

Environment Rules

  1. Grid Setup: The grid spans from x = 0 to x = width - 1 and y = 0 to y = height - 1. (0, 0) is located at the top-left corner.
  2. Move Directions: Commands consist of characters:
    • 'U' (Up): decreases y by 1 (y - 1)
    • 'D' (Down): increases y by 1 (y + 1)
    • 'L' (Left): decreases x by 1 (x - 1)
    • 'R' (Right): increases x by 1 (x + 1)
  3. Impassable Terrain: obstacles is a set of fixed (x, y) locations that no rover can enter.
  4. Collision Rules:
    • Rovers move simultaneously in discrete turn steps.
    • A rover cannot enter a space that was occupied by another rover at the beginning of the current step (no entering occupied space, even if the current occupant is trying to move away).
    • If two or more rovers attempt to enter the exact same empty cell during the same turn, none of them move; all competing rovers abort their move and stay in their current positions for that turn.
  5. Invalid Moves: If an intended move lands out of bounds, hits an obstacle, or violates a collision rule, the rover remains stationary in its current location for that turn step.

Function Signature

def simulate_rovers(grid_size: tuple[int, int], 
                    rovers: list[tuple[int, int]], 
                    commands: list[str], 
                    obstacles: set[tuple[int, int]]) -> list[tuple[int, int]]:
    pass

Constraints

  • 1 <= width, height <= 100
  • 1 <= len(rovers) <= 50
  • 0 <= len(commands[i]) <= 100
  • Instruction strings can vary in length. A rover that runs out of instructions simply stays at its current location and continues to occupy its space.

Example

grid_size = (4, 4)
rovers = [(0, 0), (2, 0)]
commands = ["DRR", "L"]
obstacles = {(1, 1)}

# Output: [(2, 1), (1, 0)]
  • Turn 1: Rover 0 moves 'D' to (0, 1). Rover 1 moves 'L' to (1, 0).
  • Turn 2: Rover 0 attempts 'R' to (1, 1) but (1, 1) is an obstacle, so it stays at (0, 1). Rover 1 has no commands left, stays at (1, 0).
  • Turn 3: Rover 0 moves 'R' to (1, 1)? No, (1, 1) is an obstacle. It tries 'R' from (0, 1) to (1, 1) - blocked! Wait, instructions for Rover 0 are "DRR":
    • Turn 1: R0 does 'D' -> (0, 1). R1 does 'L' -> (1, 0).
    • Turn 2: R0 does 'R' -> targets (1, 1) which is an obstacle -> stays at (0, 1).
    • Turn 3: R0 does 'R' -> targets (1, 1) which is an obstacle -> stays at (0, 1).