FunctionRacerProblemsRace

Fenced Robot

EasyGridsPython, JavaScript, TypeScript, Java, C++

A robot stands in the top-left cell (row 0, column 0) of a grid with rows rows and cols columns. moves is a string made of the letters U, D, L and R: up (row minus one), down (row plus one), left (column minus one) and right (column plus one). The grid is fenced: a move that would take the robot outside the grid is skipped and the robot stays where it is. Carry out the moves in order and return the final position as [row, col].

Example 1

rows 3
cols 3
moves "RRDDLU"
returns [1,1]

Right, right, down, down, left, up takes the robot from (0, 0) to (1, 1).

Example 2

rows 2
cols 2
moves "UULLR"
returns [0,1]

The first four moves would leave the 2 by 2 grid and are skipped; only the final R counts.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems