A 2-dimensional array is created from a string called matrix
:
131 673 234 103 018
201 096 342 965 150
630 803 746 422 111
537 699 497 121 956
805 732 524 037 331
After parsing, it becomes an array of arrays like this:
[
[131, 673, 234, 103, 018],
[201, 096, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 037, 331]
]
But now, the array needs to be restructured to look like this:
[
[131],
[201,673],
[630,096,234],
[537,803,342,103],
[805,699,746,965,018],
[732,497,422,150],
[524,121,111],
[037,956],
[331]
]
This new array follows the up-left
diagonals of the string or current array.
The string may need to be manipulated into one line and then rebuilt with newlines in specific positions for the current code to function properly. However, the implementation of this process is not clear at the moment.
The code used to create the array is as follows:
matrix.split("\n").reduce((a, b) =>
{
a.push(b.split(" ").map(x => parseInt(x)));
return a;
}, []);
(matrix
refers to the variable holding the string)
Ideally, only the reducer function could be replaced to achieve the desired result, but any solution would be greatly appreciated.