Index sum and strip my matrix
Given a matrix/2d array in your preferable language
Input:
- The matrix will always have an odd length
- The matrix will always be perfectly square
- The matrix values can be any integer in your language (positive or negative)
Example:
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 50 6 7 8 9
4 5 6 100 8 9 10
5 6 7 8 -9 10 11
6 7 8 9 10 11 12
7 8 900 10 11 12 0
Definitions:
- The "central number" is defined as the number that has the same amount of numbers to the left,right,up and down
In this case its middlemost 100
- The "outer shell" is the collection of numbers which their x and y index is or 0 or the matrix size
1 2 3 4 5 6 7
2 8
3 9
4 10
5 11
6 12
7 8 900 10 11 12 0
Your task:
Add to the central number the sum of each row and column after multiplying the values in each by their 1-based index
A single row for example
4 5 6 7 8
for each number
number * index + number * index.....
4*1 + 5*2 + 6*3 + 7*4 + 8*5 => 100
example:
2 -3 -9 4 7 1 5 => 61
-2 0 -2 -7 -7 -7 -4 => -141
6 -3 -2 -2 -3 2 1 => -10
8 -8 4 1 -8 2 0 => -20
-5 6 7 -1 8 4 8 => 144
1 5 7 8 7 -9 -5 => 10
7 7 -2 2 -7 -8 0 => -60
|
78 65 60 45 -15 -89 10 => 154
|
=> -16
- For all rows and columns you combine these values..
- Now you sum these too => 154-16 = 138
- You add that number to the "central number" and remove the "outer shell" of the matrix
0 -2 -7 -7 -7 => -88
-3 -2 -2 -3 2 => -15
-8 4 1+138 -8 2 => 395
6 7 -1 8 4 => 69
5 7 8 7 -9 => 26
19 69 442 30 -26
do this untill you end up with a single number
-2 -2 -3 => -15
4 1060 -8 => 2100
7 -1 8 => 29
27 2115 5
- Add 2114+2147 to 1060
- Remove the "outer shell" and get 5321
- Now we have a single number left
this is the output!
test cases:
-6
-6
-7 -1 8
-4 -6 7
-3 -6 6
2
6 7 -2 5 1
-2 6 -4 -2 3
-1 -4 0 -2 -7
0 1 4 -4 8
-8 -6 -5 0 2
-365
8 3 5 6 6 -7 5
6 2 4 -2 -1 8 3
2 1 -5 3 8 2 -3
3 -1 0 7 -6 7 -5
0 -8 -4 -9 -4 2 -8
8 -9 -3 5 7 8 5
8 -1 4 5 1 -4 8
17611
-9 -7 2 1 1 -2 3 -7 -3 6 7 1 0
-7 -8 -9 -2 7 -2 5 4 7 -7 8 -9 8
-4 4 -1 0 1 5 -3 7 1 -2 -9 4 8
4 8 1 -1 0 7 4 6 -9 3 -9 3 -9
-6 -8 -4 -8 -9 2 1 1 -8 8 2 6 -4
-8 -5 1 1 2 -9 3 7 2 5 -6 -1 2
-8 -5 -7 -4 -9 -2 5 0 2 -4 2 0 -2
-3 -6 -3 2 -9 8 1 -5 5 0 -4 -1 -9
-9 -9 -8 0 -5 -7 1 -2 1 -4 -1 5 7
-6 -9 4 -2 8 7 -9 -5 3 -1 1 8 4
-6 6 -3 -4 3 5 6 8 -2 5 -1 -7 -9
-1 7 -9 4 6 7 6 -8 5 1 0 -3 0
-3 -2 5 -4 0 0 0 -1 7 4 -9 -4 2
-28473770
This is a codegolf challenge so the program with the lowest bytecount wins
-
\$\begingroup\$ you are correct, thats a typo \$\endgroup\$downrep_nation– downrep_nation2016年06月23日 22:33:14 +00:00Commented Jun 23, 2016 at 22:33
-
3\$\begingroup\$ why would negative numbers be an issue? I dont think the challenge should adjust for esolangs but maybe the other way around is more appropriate \$\endgroup\$downrep_nation– downrep_nation2016年06月23日 23:09:41 +00:00Commented Jun 23, 2016 at 23:09
-
\$\begingroup\$ @LuisMendo I think it's not a problem, the rule "The matrix values can be any integer in your language" means to me that if your language doesn't have negative numbers, it shouldn't support them. \$\endgroup\$Fatalize– Fatalize2016年06月24日 07:53:17 +00:00Commented Jun 24, 2016 at 7:53
-
\$\begingroup\$ actually thats correct. but then the test cases wont work properly \$\endgroup\$downrep_nation– downrep_nation2016年06月24日 08:08:13 +00:00Commented Jun 24, 2016 at 8:08
-
2\$\begingroup\$ "I dont think the challenge should adjust for esolangs but maybe the other way around is more appropriate" that should be engraved in stone \$\endgroup\$edc65– edc652016年06月24日 08:43:43 +00:00Commented Jun 24, 2016 at 8:43
6 Answers 6
MATL, (削除) 36 (削除ここまで) 34 bytes
tnq?`t&+stn:*sytn2/)+ 7M(6Lt3$)tnq
Input is a 2D array with ;
as row separator
Try it online! Or verify all test cases.
Explanation
tnq % Take input. Duplicate, get number of elements, subtract 1
? % If greater than 0
` % Do...while
t % Duplicate
&+ % Sum matrix with its transpose
s % Sum each column. Gives a row vector
tn: % Vector [1 2 ...] with the same size
* % Multiply element-wise
s % Sum of vector. This will be added to center entry of the matrix
y % Duplicate matrix
tn2/ % Duplicate, get half its number of elements. Gives non-integer value
) % Get center entry of the matrix, using linear index with implicit rounding
+ % Add center entry to sum of previous vector
7M % Push index of center entry again
( % Assgined new value to center of the matrix
6Lt % Array [2 j1-1], twice. This will be used to remove shell
3$) % Apply row and col indices to remove outer shell of the matrix
tnq % Duplicate, number of elements, subtract 1. Falsy if matrix has 1 entry
% End do...while implicitly. The loop is exited when matrix has 1 entry
% End if implicitly
% Display stack implicitly
Python 2.7, 229 bytes
This is my first attempt at something like this, so hopefully I followed all the rules with this submission. This is just a function which takes in a list of lists as its parameter. I feel like the sums and list comprehension could probably be shortened a little bit, but it was too hard for me. :D
def r(M):
t=len(M)
if t==1:return M[0][0]
M[t/2][t/2]+=sum(a*b for k in [[l[x] for l in M]for x in range(0,t)]for a,b in enumerate(k,1))+sum([i*j for l in M for i,j in enumerate(l,1)])
return r([p[+1:-1]for p in M[1:-1]])
Thx to Easterly Irk for helping me shave off a few bytes.
-
1\$\begingroup\$ You can remove a couple spaces between operators (
...) + sum([i*j...
->...)+sum([i*j...
), but overall, great first post!!!! \$\endgroup\$Riker– Riker2016年06月24日 19:04:26 +00:00Commented Jun 24, 2016 at 19:04 -
\$\begingroup\$ oooh missed that. Thanks! \$\endgroup\$Jeremy– Jeremy2016年06月24日 19:21:19 +00:00Commented Jun 24, 2016 at 19:21
-
1\$\begingroup\$ Also,
...]for ...
works. You can remove at least 2 space like that. (end of list hits the for loop) \$\endgroup\$Riker– Riker2016年06月24日 19:25:21 +00:00Commented Jun 24, 2016 at 19:25
C#, 257 bytes
here is a non esolang answer
void f(int[][]p){while(p.Length>1){int a=p.Length;int r=0;for(int i=0;i<a;i++)for(int j=0;j<a;j++)r+=(i+j+2)*p[i][j];p[a/2][a/2]+=r;p=p.Where((i,n)=>n>0&&n<p.Length-1).Select(k=>k.Where((i,n)=>n>0&&n<p.Length-1).ToArray()).ToArray();}Console.Write(p[0][0]);
ungolfed:
void f(int[][]p)
{
while (p.Length>1)
{
int a=p.Length;
int r=0; //integer for number to add to middle
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++)
r +=(i+j+2)*p[i][j]; //add each element to counter according to their 1 based index
p[a / 2][a / 2] += r; //add counter to middle
p = p.Where((i, n) => n > 0 && n < p.Length - 1).Select(k => k.Where((i, n) => n > 0 && n < p.Length - 1).ToArray()).ToArray(); //strip outer shell from array
}
Console.Write(p[0][0]); //print last and only value in array
}
-
2\$\begingroup\$ Hey now, J isn't an esolang. \$\endgroup\$miles– miles2016年06月24日 08:26:21 +00:00Commented Jun 24, 2016 at 8:26
-
\$\begingroup\$ This doesn't compile if you don't include
using System.Linq
andusing System
. I'm not sure if it's required by the rules though. \$\endgroup\$Yytsi– Yytsi2016年06月24日 12:11:21 +00:00Commented Jun 24, 2016 at 12:11 -
\$\begingroup\$ its not a full program, its only a function so its ok as far as i know. i mean, would i also need to include the App.config and all the bytes in the properties and makefile? no \$\endgroup\$downrep_nation– downrep_nation2016年06月24日 12:13:01 +00:00Commented Jun 24, 2016 at 12:13
-
\$\begingroup\$ @downrep_nation It's just weird, since I've seen some people include them in the source when it has only been a function and they've included the bytes on the score. \$\endgroup\$Yytsi– Yytsi2016年06月24日 12:35:03 +00:00Commented Jun 24, 2016 at 12:35
-
\$\begingroup\$ Now when I think about it, I'm on the line that you should import atleast
System.Linq
. Other languages that require importing in order to use certain features go through the same process, so I think it's unfair to assume that every module is loaded to memory in C#. \$\endgroup\$Yytsi– Yytsi2016年06月24日 15:16:49 +00:00Commented Jun 24, 2016 at 15:16
J, 66 bytes
([:}:@}."1@}:@}.]+(i.@,~=](]+*)<.@-:)@#*[:+/^:2#\*]+|:)^:(<.@-:@#)
Straight-forward approach based on the process described in the challenge.
[:+/^:2#\*]+|:
gets the sum. ]+(i.@,~=](]+*)<.@-:)@#*
is a particularly ugly way to increment the center by the sum. [:}:@}."1@}:@}.
removes the outer shell. There probably is a better way to do this.
Usage
f =: ([:}:@}."1@}:@}.]+(i.@,~=](]+*)<.@-:)@#*[:+/^:2#\*]+|:)^:(<.@-:@#)
f _6
_6
f _7 _1 8 , _4 _6 7 ,: _3 _6 6
2
f 6 7 _2 5 1 , _2 6 _4 _2 3 , _1 _4 0 _2 _7 , 0 1 4 _4 8 ,: _8 _6 _5 0 2
_365
f 8 3 5 6 6 _7 5 , 6 2 4 _2 _1 8 3 , 2 1 _5 3 8 2 _3 , 3 _1 0 7 _6 7 _5 , 0 _8 _4 _9 _4 2 _8 ,8 _9 _3 5 7 8 5 ,: 8 _1 4 5 1 _4 8
17611
f (13 13 $ _9 _7 2 1 1 _2 3 _7 _3 6 7 1 0 _7 _8 _9 _2 7 _2 5 4 7 _7 8 _9 8 _4 4 _1 0 1 5 _3 7 1 _2 _9 4 8 4 8 1 _1 0 7 4 6 _9 3 _9 3 _9 _6 _8 _4 _8 _9 2 1 1 _8 8 2 6 _4 _8 _5 1 1 2 _9 3 7 2 5 _6 _1 2 _8 _5 _7 _4 _9 _2 5 0 2 _4 2 0 _2 _3 _6 _3 2 _9 8 1 _5 5 0 _4 _1 _9 _9 _9 _8 0 _5 _7 1 _2 1 _4 _1 5 7 _6 _9 4 _2 8 7 _9 _5 3 _1 1 8 4 _6 6 _3 _4 3 5 6 8 _2 5 _1 _7 _9 _1 7 _9 4 6 7 6 _8 5 1 0 _3 0 _3 _2 5 _4 0 0 0 _1 7 4 _9 _4 2)
_28473770
Brachylog, 114 bytes
{l1,?hh.|:{:Im:I:?:{[L:I:M]h:JmN,Ll:2/D(IJ,M{$\:?c:{:{:ImN,I:1+:N*.}f+.}a+.}:N+.;'(DIJ),N.)}f.}f:7a$\:7a&.}.
brbr.
I'm suprised this even works to be honest. At least I realized that Brachylog really needs a "change value of that element" as a built-in though...
Usage example:
?- run_from_file('code.brachylog', '[[0:_2:_7:_7:_7]:[_3:_2:_2:_3:2]:[_8:4:139:_8:2]:[6:7:_1:8:4]:[5:7:8:7:_9]]', Z).
Z = 5321 .
Explanation
More readable (and longer) version:
{l1,?hh.|:2f:7a$\:7a&.}.
:Im:I:?:3f.
[L:I:M]h:JmN,Ll:2/D(IJ,M:4&:N+.;'(DIJ),N.)
$\:?c:5a+.
:6f+.
:ImN,I:1+:N*.
brbr.
I'm just gonna explain roughly what each predicate (i.e each line except the first one which is Main Predicate + predicate 1) does:
Main predicate + predicate 1
{l1,?hh.|:2f:7a$\:7a&.}.
: If the input has only one row, then end the algorithm and return the only value. Else find all rows which satisfy predicate 2, then apply predicate 7 on the resulting matrix, then predicate 7 on the transposition, then call recursively.Predicate 2
:Im:I:?:3f.
:Take theI
th row of the matrix, find all values of that row which satisfy predicate 3 withI
and the matrix as additional inputs.Predicate 3
[L:I:M]h:JmN,Ll:2/D(IJ,M:4&:N+.;'(DIJ),N.)
:L
is the row,I
is the index of the row,M
is the matrix.N
is theJ
th element ofL
. If the length ofL
divided by 2 is equal to bothI
andJ
, then the output is the sum ofN
with the result of predicate 4 on the matrix. Otherwise the output is justN
. This predicate essentialy recreates the matrix with the exception that the center element gets added to the sum.Predicate 4
$\:?c:5a+.
: Apply predicate 5 on each row and column of the matrix, unify the output with the sum of the results.Predicate 5
:6f+.
: Find all valid outputs of predicate 6 on the row, unify the output with the sum of the resulting list.Predicate 6
:ImN,I:1+:N*.
:N
is theI
th value of the row, unify the output withN * (I+1)
.Predicate 7
brbr.
: Remove the first and last row of the matrix.
APL, 56 chars
{{1 1↓ ̄1 ̄1↓⍵+(-⍴⍵)↑(×ばつ⍴⍵)↑+/(⍵⍪⍉⍵)×ばつ⍳≢⍵}⍣(×ばつ≢⍵)⊣⍵}
In English:
⍣(×ばつ≢⍵)
repeat "half the size of a dimension rounded"-times(⍵⍪⍉⍵)×ばつ⍳≢⍵
inner product of the matrix and its transpose with the index vector(-⍴⍵)↑(×ばつ⍴⍵)↑
transform result in matrix padded with 0s1 1↓ ̄1 ̄1↓
removes outer shell