25
3
Challenge
Given a square matrix of characters (single-byte printable ASCII characters), rotate each "ring" of the matrix in opposite directions.
Let's take an example:
1 2 3 4 5
6 7 8 9 A
B C D E F
G H I J K
L M N O P
Then, the outermost ring is rotated clockwise 90 degrees, like so:
1 2 3 4 5 L G B 6 1
6 A M 2
B F => N 3
G K O 4
L M N O P P K F A 5
The second ring is rotated counterclockwise 90 degrees:
7 8 9 9 E J
C E => 8 I
H I J 7 C H
The final ring is rotated clockwise 90 degrees, but since it is a single number (letter in our example), then it is not really affected.
The final result is:
L G B 6 1
M 9 E J 2
N 8 D I 3
O 7 C H 4
P K F A 5
If the matrix has an even side length, the innermost ring will be a 2x2 square and should still be rotated.
Input
A list of lists in any reasonable standard format. For example, a newline-delimited space-delimited string or a list of space-delimited strings is acceptable, but a list of the values as rings around the matrix is not acceptable. The characters are not necessarily unique.
Output
A list of lists in any reasonable standard format. Same rules as the input.
Test Cases
1 2 3 7 4 1
4 5 6 => 8 5 2
7 8 9 9 6 3
1 2 3 4 5 6 Y S M G A 1
A B C D E F Z E K Q W 2
G H I J K L => ! D O I V 3
M N O P Q R @ C P J U 4
S T U V W X # B H N T 5
Y Z ! @ # $ $ X R L F 6
Credits
Heavily inspired by a related challenge that rotates each element counterclockwise one position (not by 90 degrees).
Related – rahnema1 – 2017-06-21T18:20:15.680
@rahnema1 Right, I remember that post. This post is mostly inspired by that one; I will credit. Thanks! – HyperNeutrino – 2017-06-21T18:21:15.477
@Mr.Xcoder Whoops. You are right, thanks. – HyperNeutrino – 2017-06-21T18:21:22.883
@HyperNeutrino can we take the dimension of the matrix as a part of input? – Uriel – 2017-06-21T19:20:46.470
All characters in your examples are unique. Will this always be the case? – Dennis – 2017-06-21T19:27:35.490
@Uriel No, I will say that your submission must figure that out itself. Sorry if that renders it impossible in your chosen language. – HyperNeutrino – 2017-06-21T22:26:48.437
@Dennis I will not guarantee that. I will specify, thanks. – HyperNeutrino – 2017-06-21T22:27:06.777
@HyperNeutrino I actually asked for C – Uriel – 2017-06-21T23:11:43.777