Tumbling 2x2 in a Matrix
code-golfmatrixinteger
Challenge:
Input: A rectangular integer matrix that's at least 2x2 in size.
Output: A 2D integer array, of the result after the top-left 2x2 block has tumbled down.
For example: Let's say we have the following 3x5 matrix as input:
[[ 4, 7,12],
[11, 2, 5],
[ 7, 3,15],
[21,10, 1],
[12, 6, 6]]
The 2x2 block is [[4,7],[11,2]]
, which will act as if it was tumbling down from a stairs (in a top-left to bottom-right direction). Here this process step-by-step:
[[ 4, 7, ],
[11, 2, ],
[--, , ],
[ , , ],
[ , , ]]
[[ , , 4],
[ ,11, 7],
[--, 2, ],
[ , , ],
[ , , ]]
[[ , , ],
[ ,11, 4],
[--, 2, 7],
[ ,--, ],
[ , , ]]
[[ , , ],
[ , , ,11],
[--, , 2, 4],
[ ,--, 7],
[ , , ]]
[[ , , ],
[ , , , ],
[--, , 2,11],
[ ,--, 7, 4],
[ , ,--]]
[[ , , ],
[ , , , ],
[--, , , , 2],
[ ,--, , 7,11],
[ , ,--, 4]]
[[ , , ],
[ , , , ],
[--, , , , ],
[ ,--, , 7, 2],
[ , ,--, 4,11]]
Doing so, it will add it's values to the other cells in its path. So here is the same step-by-step process with the other numbers added in the cells:
[[ 4, 7,12],
[11, 2, 5],
[ 7, 3,15],
[21,10, 1],
[12, 6, 6]]
[[ 4, 7,16],
[11,13,12],
[ 7, 5,15],
[21,10, 1],
[12, 6, 6]]
[[ 4, 7,16],
[11,13,16], // Note that the 13 and 5 remain the same, because the cells of the tumbling
[ 7, 5,22], // block haven't moved from the previous to this step
[21,10, 1],
[12, 6, 6]]
[[ 4, 7,16],
[11,13,16,11],
[ 7, 5,24, 4],
[21,10, 8],
[12, 6, 6]]
[[ 4, 7,16],
[11,13,16,11],
[ 7, 5,24,15], // Note that the 24 and 8 remain the same, because the cells of the tumbling
[21,10, 8, 4], // block haven't moved from the previous to this step
[12, 6, 6]]
[[ 4, 7,16],
[11,13,16,11],
[ 7, 5,24,15, 2],
[21,10, 8,11,11],
[12, 6, 6, 4]]
[[ 4, 7,16],
[11,13,16,11],
[ 7, 5,24,15, 2],
[21,10, 8,11,13], // Note that the 11 and 4 remain the same, because the cells of the tumbling
[12, 6, 6, 4,11]] // block haven't moved from the previous to this step
Challenge rules:
- I/O is flexible. You may take the input as integer-matrix, integer list with loose dimension-inputs, as a list of strings, etc. Output can modify the original input, return a new matrix, print space/newline delimiter to STDOUT, etc.
- You may optionally take the dimensions as additional input.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
- Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test case:
Input:
[[ 4, 7,12],
[11, 2, 5],
[ 7, 3,15],
[21,10, 1],
[12, 6, 6]]
Output:
[[ 4, 7,16],
[11,13,16,11],
[ 7, 5,24,15, 2],
[21,10, 8,11,13],
[12, 6, 6, 4,11]]
TODO: More to come.
Sandbox questions:
I might change the tumbling process a bit later on, since I'm not too happy about the current one. I still want the tumbling 2x2 down the matrix, but I might make the way the other values changes a bit different. This is just an initial idea.
- Any missing tags?
- Any missing rules?
- Any suggestions on how to change the output without changing the core part of the tumbling top-left 2x2 block?
- Any suggested test cases?