007 Dude, Where's My Roomba?
Imagine that you bought a bunch of Nvidia shares ten years ago, so now you live in a mansion. To help you clean the mansion, you have purchased a robotic vacuum cleaner. One day the robot is gone! Since you can't find it at any of its base stations, you assume it experienced some kind of power failure while cleaning one of the rooms-but which?
You suddenly remember that the robot sends telemetry to your home server; every time the robot moves or turns, it sends its action to the server. The floor is modeled as a grid, where each tile has a coordinate consisting of two integers X (horizontal) and Y (vertical). Moving north / south on the grid decreases / increases the Y coordinate, and moving west / east on the grid decreases / increases the X coordinate. When the robot turns, it changes direction; if it's facing north, turning right would make it face east, turning left would make it face west, and so on. Every time the robot moves forwards, it moves one tile in its current direction. For example, if the robot starts at (0, 0) and faces north, moving forward would place it at (0, -1). If it turned right and moved forward again, it would be at (1, -1).
There are obstacles blocking certain tiles; if the robot attempts to move to a tile with an obstacle in it, the robot stands still.
Given a string where each character represents an action the robot took, plus a map of obstacles, write a program that returns the coordinates of the tile the robot stopped in.
The possible moves are:
- Move forward: "F"
- Turn left: "L"
- Turn right: "R"
Assume the robot starts at (0, 0) and is initially facing north.
Here are some example inputs and the expected outputs:
Actions: "FRFFRFFRFFRF"
Obstacles: No obstacles
Expected: (0, 0)
Actions: "FRFFRFF"
Obstacles: (0, -1)
Expected: (2, 2)
Actions: "FRFRRFLF"
Obstacles: (0, -1), (1, 0)
Expected: (-1, 1)