Rotate 2D Array [duplicate]

Given an input of a 2 dimensional array of integers, rotate the array 90 degrees clockwise.

It should work for rectangular arrays of all sizes and proportions.

The rotated array should be printed at the end of the program.

Example:

Input:

[[0,1,2,3,4,5],

[6,7,8,9,0,1],

[2,3,4,5,6,7],

[8,9,0,1,2,3]]


Output:

[[8,2,6,0],
[9,3,7,1],
[0,4,8,2],
[1,5,9,3],
[2,6,0,4],
[3,7,1,5]]


Do not rely on the patterns in the arrays above, they were used just for explanation purposes. Expect the arrays to be randomly generated.

Scoring

Every character in the program counts as 1 point.

The first line of the program can store the array as a variable for 0 points. The array must be stored as-is without any changes made to it until after this first line.

If your code can rotate other multiples of 90 degrees, -15 points from your score. If this value must be stored, it can be done so at the beginning of the program for 0 points. It can be supplied in the same way that the array is supplied.

Lowest score wins!

• Will the arrays only hold numbers? Will every line in the array have the same length? Should we print the result, or output it by some other method? – xnor Jul 2 '14 at 19:33
• @xnor edited the post – erdekhayser Jul 2 '14 at 19:40
• Can "user input" be stored in a variable? Otherwise for Python, simply taking input() takes 7 characters, so there's no reasonable way to benefit from the bonus. – xnor Jul 2 '14 at 19:59
• @xnor Edited the rules. It can be created like the array now, and does not necessarily require it as input. – erdekhayser Jul 2 '14 at 20:15
• – Not that Charles Jul 2 '14 at 20:16

Python 2 (17=32-15) (19)

No input (19):

s = [[0,1,2,3,4,5],
[6,7,8,9,0,1],
[2,3,4,5,6,7],
[8,9,0,1,2,3]]
print zip(*s[::-1])


Taking input in t, the number of rotations (17=32-15)

s = [[0,1,2,3,4,5],
[6,7,8,9,0,1],
[2,3,4,5,6,7],
[8,9,0,1,2,3]]
t=3
exec"s=zip(*s[::-1]);"*t
print s


J 119 7 -10 = -3

|:&|.^:


Edit: Changing argument order gets rid of the parentheses.

Edit 2: Reducing by 2 characters by swapping rows instead of columns, and inverting the order of operations.

Adverb taking the multiple of 90 degrees, then the array to rotate.

NB. needs assigning to a variable or parentheses to separate from the arguments.
rot =: |:&|.^:
]a =: i. 4 4
0  1  2  3
4  5  6  7
8  9 10 11
12 13 14 15

|:&|.^: 1 a
12  8 4 0
13  9 5 1
14 10 6 2
15 11 7 3
|:&|.^: 2 a
15 14 13 12
11 10  9  8
7  6  5  4
3  2  1  0
|:&|.^: 3 a
3 7 11 15
2 6 10 14
1 5  9 13
0 4  8 12
|:&|.^: 4 a
0  1  2  3
4  5  6  7
8  9 10 11
12 13 14 15


Try it yourself at tryj.tk

As a little extra, this extends to any rank at the cost of 1 char, when slightly changed to

rot =: (|:|.)^:


This way it takes the permutation order for the transpose to the left, and the number of flips and the array to the right. Depending on the axis permutation requested, you get back after a number of flips: flipping two axis makes you come back after 4 turns, shifting the axis makes you come back after 2 times the rank of the array.

       ]b=:i.2 3 4
0  1  2  3
4  5  6  7
8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23
0 2 1 (|:|.)^: 1 b
12 16 20
13 17 21
14 18 22
15 19 23

0  4  8
1  5  9
2  6 10
3  7 11
1 2 0 (|:|.)^: 6 b
0  1  2  3
4  5  6  7
8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23

• The scoring for this one might be a little off. rot=:|."1&|:^: and 1 rot a would come out to 20-10 = 10, no? – erdekhayser Jul 2 '14 at 20:24
• The function itself is just the |."1&|:^: and could be used in verb frazes as such. Since it's not specified, I figured the function alone needs counting. I'll be fair and include the parens. – jpjacobs Jul 2 '14 at 20:53
• I was going to post this exact same answer. +1 – seequ Jul 2 '14 at 23:11

Ruby — 34 - 15 = 19

m = [[0,1,2,3,4,5],
[6,7,8,9,0,1],
[2,3,4,5,6,7],
[8,9,0,1,2,3]]
n = 1

n.times{m=m.reverse.transpose};p m


Haskell — 55 - 15 = 40

let m = [[0,1,2,3,4,5],[6,7,8,9,0,1],[2,3,4,5,6,7],[8,9,0,1,2,3]]
let n = 1 :: Int

import Data.List
print\$(iterate(transpose.reverse)m)!!n


Javascript (ES6) 5148 45

Alerts each row of the new rotated array in sequence.

a = [[0,1,2,3,4,5],
[6,7,8,9,0,1],
[2,3,4,5,6,7],
[8,9,0,1,2,3]]


• You can use a.map(j=>j[i]) instead of [j[i]for(j of a)] to save 3 more characters ;) – core1024 Jul 3 '14 at 7:41