Robot Crossing Yard
MediumOnline Assessment
Move a fleet of warehouse robots that follow written programs and must not run into each other, then report where each ended up and how often it was held up.
The floor is a grid given as grid, a list of equal-length strings: . is an open cell and # a shelf. Robot i starts at robots[i] = [row, col] (all start cells are open and distinct) and follows programs[i], a string of commands, one per tick: U, D, L, R (up, down, left, right) or S (stay). A robot whose program is used up stays where it is for the remaining ticks. The simulation runs for as many ticks as the longest program.
In each tick every robot tries to carry out its next command at the same time. A robot that does not move, because of S, an exhausted program, or being blocked, occupies its own cell. A robot that tries to move is blocked (it stays in place) if any of these holds:
- its target cell is outside the grid or is a
#; - another robot has the same target cell (all robots aiming at that cell are blocked);
- its target cell is the cell of a robot that does not move in this tick, including a robot that is blocked in this very tick;
- it and one other robot are trying to swap cells with each other (both are blocked).
Moving into a cell that another robot is leaving in the same tick is allowed, so a line of robots can advance together, and so can a ring of three or more robots rotating. Because being blocked can block others, keep applying these rules until no more robots become blocked; the robots that are then still moving all move.
A robot is held up in a tick if it had a U, D, L or R command and was blocked. S is never a hold-up.
Input: grid; robots, the start cells; programs, one string per robot.
Output: a list with one entry per robot, [row, col, held_up]: its final cell and the number of ticks in which it was held up.
Example 1
- Input:
- [["....",".#..","...."],[[0,0],[0,2],[2,1]],["RRD","LSS","UL"]]
- Output:
- [[0,1,2],[0,2,1],[2,0,1]]
- Explanation
- In tick 1, robots 0 and 1 both aim at cell (0, 1), so both are blocked; robot 2 tries to go up into a shelf and is blocked too. In tick 2 robot 0 moves right to (0, 1), robot 1 stays, and robot 2 moves left to (2, 0). In tick 3 robot 0 tries to go down into the shelf at (1, 1) and is blocked, so it ends at (0, 1) held up twice; robot 1 was held up once and robot 2 once.
Example 2
- Input:
- [["....","...."],[[0,0],[0,1],[0,2]],["RR","RR","RR"]]
- Output:
- [[0,1,1],[0,2,1],[0,3,1]]
- Explanation
- In tick 1 all three robots move right in a line: each enters the cell the next one is leaving. In tick 2 the front robot would leave the grid, so it is blocked; the middle robot's target is now occupied by a robot that does not move, so it is blocked; so is the last one. Every robot is held up exactly once.
Example 3
- Input:
- [["....","...."],[[0,0],[0,1],[1,1],[1,0],[0,2],[0,3]],["R","D","L","U","RR","LS"]]
- Output:
- [[0,1,0],[1,1,0],[1,0,0],[0,0,0],[0,2,2],[0,3,1]]
- Explanation
- The first four robots sit on a 2x2 block and move clockwise (R, D, L, U): each moves into the cell its neighbour leaves, so the ring turns and nobody is held up. Robots 4 and 5 try to swap cells in tick 1 and are both blocked. In tick 2 robot 4 aims right at robot 5's cell, and robot 5 stays (its command is S), so robot 4 is blocked again: held up twice, robot 5 once.
Constraints
- 1 ≤ grid.length, grid[0].length ≤ 8; every row has the same length
- 0 ≤ robots.length ≤ 6; start cells are open and distinct
- programs.length = robots.length; each program has at most 30 characters from UDLRS