Given a matrix, sum its values up/down or left/right to form an X, fold it up, and return the list. I describe the algorithm here:
Algorithm
Your input will be a odd-sized square matrix of integers within your language's reasonable numerical capacity.
Let's take the following matrix as an example:
1 2 3 2 1
0 3 2 3 0
4 2 5 6 3
7 4 7 9 4
0 6 7 2 5
First, add every number to the closest number that is on the main diagonal or antidiagonal. That is, divide the matrix into four sections along the main diagonal and antidiagonal, and then sum all of the numbers in each section towards the center, like so:
1 2 3 2 1
↓ ↓ ↓
0 → 3 2 3 ← 0
↓
4 → 2 → 5 ← 6 ← 3
↑
7 → 4 7 9 ← 4
↑ ↑ ↑
0 6 7 2 5
This step gives the following result:
1 1
5 5
39
17 15
0 5
Then, we fold it by flattening the X and interweaving the elements with the top-left first and the bottom left last. This gives the following result:
1, 0, 5, 17, 39, 5, 15, 1, 5
You can imagine this as stretching the main diagonal and rotating it counterclockwise.
This is the final result.
Challenge
Implement this algorithm. Standard loopholes apply. All reasonable I/O formats acceptable.
Test Cases
Input
Output
1 2 3 2 1
0 3 2 3 0
4 2 5 6 3
7 4 7 9 4
0 6 7 2 5
1, 0, 5, 17, 39, 5, 15, 1, 5
1 2 3 4 5
5 4 3 2 1
1 3 5 7 9
0 9 8 7 6
6 7 8 9 0
1, 6, 11, 16, 47, 7, 22, 5, 0
1 3 7 4 8 5 3
8 4 7 5 3 8 0
0 6 3 6 9 8 4
2 6 5 8 7 4 2
0 6 4 3 2 7 5
0 6 7 8 5 7 4
8 5 3 2 6 7 9
1, 8, 15, 11, 23, 20, 62, 32, 25, 13, 18, 3, 9