27
\$\begingroup\$

Take a square matrix containing positive integers as input, and calculate the "rotated sum" of the matrix.

Rotated sum:

Take the sum of the original matrix and the same matrix rotated 90, 180 and 270 degrees.

Suppose the matrix is:

 2    5    8
 3   12    8
 6    6   10

then the rotated sum will be:

2    5    8     8    8   10    10    6    6     6    3    2
3   12    8  +  5   12    6  +  8   12    3  +  6   12    5  = 
6    6   10     2    3    6     8    5    2    10    8    8   

26   22   26
22   48   22
26   22   26

Test cases:

Input and output separated by dashes, different test cases separated by a newline. Test cases in more convenient formats can be found here.

1
-------------
4

1 3
2 4
-------------
10   10 
10   10    

14    6    7   14
 6   12   13   13
 6    2    3   10
 5    1   12   12
-------------
45   37   24   45
24   30   30   37
37   30   30   24
45   24   37   45    

14    2    5   10    2
18    9   12    1    9
 3    1    5   11   14
13   20    7   19   12
 2    1    9    5    6
-------------
24   29   31   41   24
41   49   31   49   29
31   31   20   31   31
29   49   31   49   41
24   41   31   29   24

Shortest code in bytes in each language wins. Explanations are highly encouraged!

\$\endgroup\$

33 Answers 33

1
2
0
\$\begingroup\$

Perl 5, 92 bytes

sub f{@t=@_;map{//;[map$t[$'-1][$_-1]+$t[$_-1][-$']+$t[-$'][-$_]+$t[-$_][$'-1],1..@t]}1..@t}

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Burlesque, 38 bytes

SP{{}{tp<-}{<-)<-}{tp)<-}}M-r{q?+Z]}sp

Try it online!

SP        # Read input as array
{
 {}       # Original
 {tp<-}   # 90deg 
 {<-)<-}  # 180deg
 {tp)<-}  # 270deg
}
M-        # Return an array of maps
r{        # Reduce by
 q?+      # Vectorised addition
 Z]       # Zip, push, map
}
sp        # Pretty print as array
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

GolfScript, 64 bytes

4/{.(,\;[]:w;{[]:t;{(t\+:t;}%t{+}*w\+:w;}\*; w.,(!!3*)*.4/,/~}%`

I cheated, I cheated. Kinda. Though there are typical, standardized ways to take in Matrix input, I decided to take it in a... less conventional method. GolfScript is fucking terrible at rotational mathematics, so I knew I had to try. Below is the array-matrix method I used.

I am oh, so very sorry for the disaster that is this array formatting.

So yeah. You might notice a bit of convenient symmetry in this format, and I abused the hell out of it.

Input is taken in as an array of circular-ordered arrays, such as

[ [1 2 3 4][5 6 7 8][9 10 11 12][13 14 15 16][17 18][19 20][21 22][23 24][25] ]

for a 5x5 array.

1  2  3  4  5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

It returns in identical output.

Teehee.

Maybe one day I'll make a program that takes an array of this format and turns it into nice formatting and vice-versa.

Then my program will no longer be an abomination.

I mean, it still would be, the I/O would just be more conventional.

God, should I even write an explanation for this hot garbage?

Try it online, if you dare.

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.