Turn Every Ring
grid is a list of rows; its number of rows and its number of columns are both even. Split it into rings: ring 0 is made of the cells on the outer edge, ring 1 of the edge cells of what remains after removing ring 0, and so on until no cells are left. Turn every ring k steps counterclockwise. In one step every value moves to the next cell of its ring in the counterclockwise direction: values on the top side move left, values on the left side move down, values on the bottom side move right and values on the right side move up, with the corner values turning the corner. Each ring turns on its own. Return the resulting grid as a new grid, leaving grid itself unchanged.
Example 1
In the outer ring the 2 moves left into the corner, the 1 turns the corner and moves down, the 13 moves right and the 16 moves up. The inner ring of four cells turns one step the same way.
Example 2
The only ring has 4 cells, so 6 steps are the same as 2 steps, which is half a turn.
Constraints
2 <= rows, cols <= 10, and both are even-100 <= grid[r][c] <= 1000 <= k <= 10^9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Zigzag DiagonalsMedium
- Darken the BoardHard
- Flags in OrderHard
- Island ShapesHard
- Largest Island After One FillHard
- Last Dry CrossingHard
- Longest ClimbHard
- Plow Every CellHard