A security patrol unit walks along the outer boundary of a rectangular compound that has been mapped onto a grid with corners at (0,0) and (width-1, height-1). The unit starts stationed at (0,0) facing East, and it only ever moves along the perimeter of the rectangle, one grid unit at a time, in a fixed counterclockwise loop:
and then the loop repeats indefinitely.
You will receive a sequence of advance orders. Each order tells the unit to move forward some number of grid units along this loop (continuing smoothly through corners, changing facing as needed). After each order, report the unit's resulting coordinates and the direction it is now facing. Note that whenever the unit returns to (0,0) after having moved at all, it is treated as still facing South (the direction it was walking when it arrived there) rather than East -- it only faces East again once it has actually advanced past (0,0) into the bottom edge.
Line 1 contains two integers width and height.
Line 2 contains a single integer q, the number of advance orders.
Each of the next q lines contains one integer steps, the number of grid units to advance for that order.
Print q lines. The i-th line contains the unit's coordinates and facing direction after applying the first i orders in sequence, formatted as x y dir where dir is one of North, East, South, West.
Example 1
Input
3 2 3 2 1 4
Expected
2 0 East 2 1 North 1 0 East
Explanation
The compound spans x in [0,2], y in [0,1], so the loop has length 2*2+2*1=6 (segments of 2,1,2,1 for East,North,West,South). Order 1 advances 2 steps from (0,0): straight along the bottom edge to (2,0), still facing East. Order 2 advances 1 more step (cumulative 3): that lands at the corner (2,1), now facing North. Order 3 advances 4 more steps (cumulative 7, which is 1 past a full loop of 6): after wrapping once back to (0,0) facing South, one more step along the bottom edge lands at (1,0) facing East.
Example 2
Input
2 3 2 5 7
Expected
0 1 South 0 0 South
Explanation
Here x in [0,1], y in [0,2], so the loop length is 2*1+2*2=6 (segments 1,2,1,2). Order 1 advances 5 steps: East segment length 1, North segment length 2 (cumulative 3), West segment length 1 (cumulative 4), then 1 more step into the South segment lands at (0,1) facing South. Order 2 advances 7 more steps (cumulative 12, which is exactly two full loops of 6): a full loop always ends the South segment exactly back at (0,0), so the unit is at (0,0) still facing South.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →