Trace parcel deflectors
Your task
A parcel sorter routes packages over a grid of fixed deflectors, one deflector per cell. A cell holds '>' to push a parcel one cell right, '<' one cell left, '^' one cell up, 'v' one cell down, or 'O' for a chute that drops the parcel out of the machine. A parcel is placed on the cell at start_row and start_col, then follows whatever deflector it lands on. Return how many cells the parcel rests on, counting the one it starts on. The run ends at a chute, which counts, or when a deflector pushes the parcel off the belt, which does not. Return -1 when the parcel reaches a cell it has already rested on, since it then circles forever. Return 0 when the belt has no cells, or when the start is off the belt. Every row has the same width.
Examples
sorter_path_length(belt: list[list[str]], start_row: int, start_col: int) -> intExample 1
Input
sorter_path_length([[">", ">", "O"]], 0, 0)Output
3Example 2
Input
sorter_path_length([[">", "<"]], 0, 0)Output
-1Example 3
Input
sorter_path_length([["^", "O"]], 0, 0)Output
1