KatInterview

Shortest Looping Command Script for a Fuel-Limited Rover

Trees & GraphsAuto-graded · 17 test cases
HardAuto-gradedTrees & Graphssimulationbrute-forcegrid

Last updated

Brute-force enumerate looping command scripts of length 1 to 10 and simulate each on a grid with fuel, walls and refuel pads, using state-repeat detection to stop loops.

A rover sits on a rectangular grid. You must find a short command script that, when repeated over and over, drives the rover into the rightmost column.

Grid

grid is a list of equal-length strings, one per row. Each character is one of:

  • . is an open cell.
  • # is a wall.
  • F is a refuel pad (it is also an open cell the rover can enter).

start is [row, col], the rover's starting cell. It is not a wall and is not in the last column.

Rover and script

  • The rover starts with 100 fuel units.
  • A script is a string of length 1 to 10 over the letters U, D, L, R, F.
  • The rover executes the script's commands in order. After the last command it returns to the first one, so the script loops indefinitely.
  • U, D, L, R move one cell up (row - 1), down (row + 1), left (col - 1) or right (col + 1), and cost 1 fuel.
  • F refuels. It is only allowed while the rover stands on a refuel pad, adds exactly 50 fuel, and does not move. There is no fuel cap, and a pad may be used any number of times.

When a script fails

A script fails as soon as any of these happens:

  • A movement command would leave the grid or enter a wall.
  • A movement command is issued while fuel is 0. Fuel is checked before the move, so a rover with 0 fuel cannot move, but it can still execute F on a pad.
  • F is executed on a cell that is not a refuel pad.
  • The rover never reaches the last column, i.e. it repeats forever. Equivalently, the same combination of (rover cell, position in the script) occurs a second time.

A script succeeds the moment the rover is in the last column. This is checked before executing each command.

Task

Return the script that succeeds and is chosen by this rule:

  1. Prefer the shortest script.
  2. Among scripts of the same length, take the one that is smallest in dictionary order using the letter order U < D < L < R < F.

If no script of length 1 to 10 succeeds, return None.

Constraints

  • 1 <= len(grid) <= 6, 1 <= len(grid[0]) <= 8, and all rows have equal length.
  • start is a valid, non-wall cell not in the last column.
  • Scripts have length between 1 and 10 inclusive.
  • Output is a string of letters from UDLRF, or None.

Examples

Example 1

Input: find_rover_script(['...'], [0, 0])

Output: 'R'

Example 2

Input: find_rover_script(['.#.', '...'], [0, 0])

Output: 'DRR'

Example 3

Input: find_rover_script(['...', 'F##'], [1, 0])

Output: 'URR'

Hints

Hint 1

The number of candidate scripts is at most 5 + 5^2 + ... + 5^10, which is manageable if you generate them in length order and, within a length, in the required letter order, stopping at the first success.

Hint 2

To simulate one script, track (row, col, fuel, instruction index). Check for success before each command, and fail on any illegal move or illegal F.

Hint 3

A script can loop forever. Remember each (row, col, instruction index) seen; seeing one again means the rover is in a cycle that never reaches the last column, so the script fails.