# The Challenge

Given a n x n matrix of integers with n >= 2

1 2
3 4


and a list of integers with exactly 2nelements

[1,2,-3,-1]


output the rotated matrix. This matrix is contructed in the following way:

• Take the first integer in the list and rotate the first row to the right by this value.
• Take the next integer and rotate the first column down by this value.
• Take the next integer and rotate the second row to the right by this value, etc. until you rotated every row and column of the matrix once.

The list can contain negative integers which means that you shift the row/column left/up instead of right/down. If the integer is zero, don't rotate the row/column.

Example using the input above

List element      Matrix         Explanation
------------------------------------------------------------
1                  2 1           Rotate 1st row right by 1
3 4

2                  2 1           Rotate 1st column down by 2
3 4

-3                 2 1           Rotate 2nd row left by 3
4 3

-1                 2 3           Rotate 2nd column up by 1
4 1


# Rules

• You may choose the most conventient input format. Just make it clear which one you use.
• Function or full program allowed.
• Default rules for input/output.
• Standard loopholes apply.
• This is , so lowest byte-count wins. Tiebreaker is earlier submission.

# Test cases

Input format here is a list of lists for the matrix and a normal list for the integers.

[[1,2],[3,4]],[1,2,-3,-1] -> [[2,3],[4,1]]
[[1,2],[3,4]],[1,1,1,1] -> [[3,2],[4,1]]
[[1,2],[3,4]],[0,0,0,0] -> [[1,2],[3,4]]
[[1,2,-3],[-4,5,6],[7,-8,0]],[1,-2,0,-1,3,4] -> [[7,5,0],[-3,-8,2],[-4,1,6]]
[[1,2,-3],[-4,5,6],[7,-8,0]],[3,12,-3,0,-6,-3] -> [[1,2,-3],[-4,5,6],[7,-8,0]]


Happy Coding!

• mapcar should help… Commented Mar 6, 2016 at 6:33

## CJam, 13 bytes

{{a.m>1m<z}/}


An unnamed block (function) that takes the matrix and the list on top of the stack (in that order) and leaves the new matrix in their place.

Run all test cases.

Same idea, same byte count, different implementations:

{{\(@m>a+z}/}
{{(Im>a+z}fI}
{{:\Im>]z}fI}


### Explanation

Ideally we want to treat each instruction in the list the same, and just use it to rotate the first row of the matrix. This can be done quite easily by transforming the matrix after each instruction a bit and making sure that all those extra transformations cancel out in the end. So after processing each instruction, we rotate all rows one up (such that the next instruction along the same dimension processes the next row) and then transpose the matrix, such that we're actually processing columns next. These additional transformations are orthogonal to the instructions in the list and have a period of exactly 2n, just what we need.

As for the code:

{      e# For each instruction...
a    e#   Wrap it in a singleton array.
.m>  e#   Combine it element-wise with the matrix to rotate right. This is
e#   a fairly common idiom to apply a binary operation only to the first
e#   element of an array, since element-wise operations just retain all the
e#   unpaired elements of the longer array.
1m<  e#   Rotate the rows one up.
z    e#   Transpose.
}/


# APL (Dyalog Extended), 171514 13 bytes

(⍉1⊖⌽@1⍢⌽)/⌽


Try it online!

Takes input as a list where the first element is the matrix, and the remaining elements are the rotation amounts. If ⌽ rotated to the right instead of left, this would beat CJam.

(⍉1⊖⌽@1 1⍢⌽)/⌽    Monadic train:
(⍉1⊖⌽@1 1⍢⌽)      Helper function to rotate and transpose once.
Takes args ⍺ (amount to rotate) and ⍵ (current array)
⌽                 Function to rotate left
1 1             2-element vector containing 1.
The second 1 is redundant, but saves 1 byte over (,1).
⌽@1 1             Function to rotate the 1st row left by ⍺.
⌽@1 1⍢⌽          Reverse ⍵, rotate 1st row left by ⍺, then reverse again.
This rotates the first row of ⍵ to the *right*.
1⊖                   Rotate all the rows upward,
⍉                                                 then transpose.
(⍉1⊖⌽@1 1⍢⌽)/⌽   Fold (/) this function over the reversed input.
If our input is ⍵, ⍺_1, ⍺_2, ..., ⍺_2n,
the reversed input will be ⍺_2n, ..., ⍺_1, ⍵.
The / operator applies the function right to left,
so the ⌽ is necessary.

• {⍉1⊖⌽⍺⌽@(⍳1)⌽⍵}(⍉1⊖⌽@(⍳1)⍢⌽) but can you explain to me why @1 won't work instead of @(⍳1) or @(,1)? Also, OP may allow you to take input in reverse.
Commented Feb 11, 2019 at 10:51
• So it turns out that dfns' at which is behind the extended @ isn't a compatible extension. However, you can use the native @ with @1 which saves a byte over @1 1.
Commented Feb 12, 2019 at 21:36
• does rotate work differently in apl vs J ? i’m trying to translate this to J and finding it a lot more verbose... Commented Feb 16, 2019 at 20:32

## Python 2, 96 bytes

def f(m,v):
for i,x in enumerate(v):x%=len(m);r=m[i/2];m[i/2]=r[-x:]+r[:-x];m=zip(*m)
return m


Tests

f returns a list of tuples. Each line in the function body is indented with 1 tab character.

• Wouldn't a straight return zip(*m) save you 5 bytes? Commented Mar 6, 2016 at 21:42
• @DenkerAffe: are you suggesting to replace m=zip(*m);return m with just return zip(*m)? I can't do that because m=zip(*m) is a part of the for loop Commented Mar 7, 2016 at 14:55
• Yea, that was my though. Didn't see that its in the loop, my bad. Commented Mar 7, 2016 at 15:20

# Stax, 12 bytes

ÄBí•»O╒┬g½ë
`

Run and debug it