APL (Dyalog Unicode), 37 35 bytes
{⌽∘⍉@⍺⊢⍵}/⌽(⊂,{(4|⎕)/,⊢∘⊂⌺2 2⍳⍴⍵})⎕
Switched to a more straightforward method after I realized @
also accepts a matrix of coordinates. Then we don't need to fiddle with the coordinate order; we extract the submatrix coordinates with ⊢∘⊂⌺2 2
, and just rotate them directly using ⌽∘⍉
.
APL (Dyalog Unicode), 37 bytes
{1⌽@⍺⊢⍵}/⌽(⊂,{(4|⎕)/,2,∘⌽/2,⌿⊂¨⍳⍴⍵})⎕
A full program that takes the matrix, then the vector of rotation amounts. Prints the resulting matrix with a leading space.
@
can extract the elements at certain positions, manipulate them, and place them back into the original matrix, which is great for 2×2 rotation. In particular, 1⌽@(1 1)(2 1)(2 2)(1 2)
extracts the top left submatrix [a b][c d]
into a vector a c d b
, rotates once to the left (1⌽
) into c d b a
, then puts the values back so that the submatrix becomes [c a][d b]
. This achieves rotating the submatrix exactly once.
{1⌽@⍺⊢⍵}/⌽(⊂,{(4|⎕)/,2,∘⌽/2,⌿⊂¨⍳⍴⍵})⎕
⍝ Read from right:
⎕ ⍝ Take the matrix from stdin
{...} ⍝ Pass to the dfn as ⍵
⍳⍴⍵ ⍝ Matrix of 2D coordinates of ⍵
2,⌿⊂¨ ⍝ Pair vertically adjacent coordinates
2,∘⌽/ ⍝ Catenate horizontally adjacent coordinate pairs,
⍝ flipping the right one so that it looks like (1 1)(2 1)(2 2)(1 2)
, ⍝ Flatten the matrix of lists of coordinates
(4|⎕)/ ⍝ Copy each (Rotations modulo 4) times
⌽(⊂,...) ⍝ Prepend the original matrix enclosed and reverse the entire array,
⍝ so that it is suitable for RTL reduce
{ }/ ⍝ RTL reduce by...
1⌽@⍺⊢⍵ ⍝ Take the matrix ⍵ and rotate once at coordinates ⍺