Let's say I have the following (2D) matrix:
[[1, 2, 3, 4 ],
[5, 6, 7, 8 ],
[9, 10, 11, 12],
[13, 14, 15, 16]]
Rotate the matrix counterclockwise R
times (not in 90 degree increments, just by 1 number each time),
1 2 3 4 2 3 4 8 3 4 8 12
5 6 7 8 --> 1 7 11 12 --> 2 11 10 16
9 10 11 12 5 6 10 16 1 7 6 15
13 14 15 16 9 13 14 15 5 9 13 14
Completed example:
Input:
2
[[1, 2, 3, 4 ],
[5, 6, 7, 8 ],
[9, 10, 11, 12],
[13, 14, 15, 16]]
Output:
[[3, 4, 8, 12],
[2, 11, 10, 16],
[1, 7, 6, 15],
[5, 9, 13, 14]]
(weird spaces are to align the numbers in nice columns)
The outer "ring" of the matrix rotates 2 counterclockwise, and the inner right rotates 2 also. In this matrix, there are only two rings.
An example with 1 "ring":
2
[[1, 2],
[3, 4],
[5, 6]]
Should output:
[[4, 6],
[2, 5],
[1, 3]]
Your challenge is to take in a matrix and an integer R
, and output the translated version after R
rotations.
Rotation of a 4x5 matrix is represented by the following figure:
Constraints:
2 ≤ M, N ≤ 100
, where M and N are the dimensions of the matrix. It is guaranteed that the minimum of M and N will be even.1 ≤ R ≤ 80
, where r is number of rotations.- The matrix will only ever contain positive integers.
- Values are not always distinct.
- The input should always be as a 2D array (if you can't take runtime input as a 2D array, then you just have to find another way to get input).
Another test case, with non-distinct values:
1
[[1, 1],
[2, 2],
[3, 3]]
Outputs:
[[1, 2],
[1, 3],
[2, 3]]
This is code-golf, so the shortest answer wins!
[[3, 4, 8, 12], [2, 11, 10, 16], [1, 7, 6, 16], [5, 9, 13, 14]]
the 16 is suddenly duplicated I guess it should be:[[3, 4, 8, 12], [2, 11, 10, 16], [1, 7, 6, 15], [5, 9, 13, 14]]
? \$\endgroup\$