| Bytes | Lang | Time | Link |
|---|---|---|---|
| 012 | Uiua | 241014T004052Z | noodle p |
| 094 | Python 2 | 190412T013444Z | vo1stv |
| 020 | Pyth | 151113T231525Z | isaacg |
| 030 | J | 151113T201632Z | randomra |
| 085 | Octave | 151114T051442Z | alephalp |
| 024 | J | 151114T042930Z | algorith |
| 139 | Julia | 151113T235800Z | Alex A. |
| 075 | ES6 | 151114T010949Z | Neil |
| 020 | CJam | 151113T180154Z | aditsu q |
| 040 | CJam | 151113T163724Z | Martin E |
Uiua, 14 12 bytes
⍜⊕□⍚↻₁/+°⍉°⊡
Try it: Uiua pad
Take the addition table of the same shape, use this to group the input, rotate each group by one to the right, and ungroup back to a matrix.
Python 2, 113 104 94 bytes
f=lambda i,b=[]:i and[b and b[:1]+i[0][:-1]]+f(i[1:],b[1:]or i[0][:-1]+[l[-1]for l in i])or[b]
This is a fairly literal interpretation of @aditsu's method. Python's syntax for treating empty lists as False helped save an extra 10 bytes.
Pyth, 20 bytes
J+PhQ.)MQ++L.(J0tQ]J
Uses Adistu's approach of removing the top row and right column, then sticking them on the left and bottom. But with mutable data structures, not transpositions.
J, 38 30 bytes
8 bytes saved thanks to @algorithmshark.
{./.((}.~#),~({.~#),.])}:"1@}.
The function collects the top and left edges into a list, cuts the list to two pieces of sufficient sizes and stitches them to the right and bottom of the core part.
Usage:
]input=.0 1 2 3, 4 5 6 7,: 8 9 0 1
0 1 2 3
4 5 6 7
8 9 0 1
({./.((}.~#),~({.~#),.])}:"1@}.) input
0 4 5 6
1 8 9 0
2 3 7 1
Octave, 85 bytes
@(a)[(b=[a(1,1:end),a(2:end,end)'])(1:(s=size(a)(1)))',[a(2:end,1:end-1);b(s+1:end)]]
I hope I could get rid of the ends.
J, 24 char
Function taking one argument.
$$<@(1&|.)/./:&;</.@i.@$
J has an operator /. called Oblique. It can't invert it, so reconstruction isn't trivial, but you can consider "listing obliques" as a permutation of the elements of the array. So we invert that permutation with /: (dyadic Sort), by putting the "listing obliques" permutation for that size (</.@i.@$) on the right and our new oblique values, rotated properly, on the left. Then we reshape this list into the old rectangular array using good old $$.
3 4$i.10
0 1 2 3
4 5 6 7
8 9 0 1
($$<@(1&|.)/./:&;</.@i.@$) 3 4$i.10
0 4 5 6
1 8 9 0
2 3 7 1
Julia, 153 149 139 bytes
A->(length(A)>1&&((m,n)=size(A);r(X)=for i=1:n X[:,i]=reverse(X[:,i])end;r(A);for i=-m:m A[diagind(A,i)]=circshift(diag(A,i),1)end;r(A));A)
This creates an unnamed function that accepts an array and returns the input array modified in place.
Ungolfed:
# Create a function to reverse the columns of a matrix
function revcols!(X)
for = 1:size(X, 2)
X[:,i] = reverse(X[:,i])
end
return X
end
# Our main function
function zgarb!(A)
# Only perform operations if the array isn't one element
if length(A) > 1
# Record the number of rows
m = size(A, 1)
# Reverse the columns in place
revcols!(A)
# Shift each diagonal
for i = -m:m
A[diagind(A, i)] = circshift(diag(A, i), 1)
end
# Reverse the columns back
revcols!(A)
end
return A
end
Thanks to Martin Büttner for algorithmic advice and for saving 4 bytes!
ES6, 75 bytes
This accepts an array of arrays as a parameter and modifies it in place.
a=>{t=a.shift();a.map(r=>{t.push(r.pop());r.unshift(t.shift())});a.push(t)}
Ungolfed:
function anti_diagonal(array) {
var temp = array.shift(); // strip off the first row
array.forEach(row => temp.push(row.pop())); // strip off the last elements of each row
array.forEach(row => row.unshift(temp.shift())); // distribute the elements to the beginning of each row
array.push(temp); // the remaining elements become the last row
}
See @aditsu's diagram for further clarification.
CJam, 20
{z_)\zLa+(@+\.+s\,/}
Written as a function block. Try it online
Explanation:
The input can be viewed like this:
That is, we separate the top row and right column from the rest of the matrix, and consider those elements in the order shown by the arrow.
Then the output is like this:
The remaining rectangular block is moved diagonally as a whole, and the edge elements are rearranged in the order/positions showed by the new arrow.
The code does almost exactly that, except the output is first generated with the arrow going straight down (so the matrix has a tail, like the letter P), then corrected.
z zip (transpose) the matrix
_ make a copy
) take out the last row (right column before transposing)
\ swap with the rest of the matrix
z transpose back
La+ append an empty row (needed for the single-column case,
which leaves an empty matrix here)
( take out the first row (top row without the corner)
@+ bring the right column to the top of the stack and concatenate
obtaining an array of the edge elements (marked with the blue arrow)
\ swap with the remaining part (big white block in the diagrams)
.+ concatenate element by element
each edge element is concatenated with a row of the white block
after the white block runs out, the remaining elements form new rows
s convert the whole thing to a string (concatenating all rows)
\ swap with the copy of the transposed matrix
, get its length (number of original columns)
/ split the string into rows of that length
CJam, 44 43 42 40 bytes
qN/:ReeSf.*:sz1fm<{Rz,{(S-(o\}*~]{},No}h
Hmm, much better than my first attempt, but I have a feeling Dennis will solve this in much less anyway...
Input and output are as ASCII grids:
0123
4567
8901
gives
0456
1890
2371

