From this stackoverflow question
Given a 2D array of size \$ M \times N \$, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be \$(0,0)\$.
Example Given:
$$ \begin{bmatrix} \color{blue}1&\color{red}2&\color{red}3&\color{red}4 \\ \color{red}5&6&7&\color{red}8 \\ \color{red}9&10&11&\color{red}{12} \\ \color{red}{13}&\color{red}{14}&\color{red}{15}&\color{red}{16}\end{bmatrix} $$
The edge values in counterclockwise is then \$ 1,5,9,13,14,15,16,12,8,4,3,2 \$.
Now we repeat the process for the inner values. This will end up with a matrix like the following
$$ \begin{bmatrix} \color{blue}6&\color{red}7 \\ \color{red}{10}&\color{red}{11} \end{bmatrix}$$
And the inner values is then \$ 6,10,11,7 \$
The final result will be then \$ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 \$
Rules
- Assume non-empty input
- Assume matrix values as positive integers
- Standard I/O Methods apply
- Standard code-golf rules and winning criteria apply
Some test cases
Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13
--------------------------------------------------------
Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8
-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6