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â€¦ – msh210 Mar 6 '16 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. – Adám Feb 11 '19 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. – Adám Feb 12 '19 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... – Jonah Feb 16 '19 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? – Denker Mar 6 '16 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 – vaultah Mar 7 '16 at 14:55
• Yea, that was my though. Didn't see that its in the loop, my bad. – Denker Mar 7 '16 at 15:20

Stax, 12 bytes

Ă„BĂ­â€˘Â»Oâ•’â”¬gÂ˝Ă«
`

Run and debug it