Turtle Commands
grid is a list of equal-length strings, one per row, top row first: # is a wall, . is open floor and T marks the cell where a turtle starts (there is exactly one T, and that cell is open floor too). The turtle starts facing up, towards row 0. commands is a string of the letters L, R and F: L turns the turtle a quarter turn to its left, R a quarter turn to its right, and F moves it one cell forward in the direction it is facing. A forward move that would leave the grid or enter a wall is skipped: the turtle stays where it is and keeps facing the same way. Return the turtle's final position as [row, col]. Do not change grid.
Example 1
The turtle turns right and steps to column 1, turns back to face up, bumps into the wall in row 1 and stays put, then turns right again and steps to column 2.
Example 2
Moving up and then moving left would both leave the grid, so both moves are skipped. After the second left turn the turtle faces down and steps into row 1.
Constraints
1 <= rows, cols <= 10- every row has the same length
- exactly one character of
gridisT 0 <= len(commands) <= 60commandshas only the lettersL,RandF
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Up and to the RightEasy
- Wireworld StepEasy
- Crush and FallMedium
- Four in a RowMedium
- Life-Like GenerationsMedium
- Minutes to BurnMedium
- Paths Around RocksMedium
- Ramp DropMedium