Challenge:
Given an NxN matrix where \$N\geq2\$ and one of eight distinct 'folding options', output a 2D array/list with the subtracted values.
The eight folding options are: left-to-right; right-to-left; top-to-bottom; bottom-to-top; topleft-to-bottomright; topright-to-bottomleft; bottomleft-to-topright; bottomright-to-topleft.
Step by step examples:
Input matrix:
[[ 1, 3, 5, 7],
[ 0, 8, 6, 4],
[ 1, 1, 1, 1], (a'th row in the explanation below)
[ 1,25, 0,75]]
With folding option top-to-bottom we output the following as result:
[[ 1,-7,-5,-3],
[ 0,22,-5,68]]
Why? We fold from the top to the bottom. Since the matrix dimensions are even, we don't have a middle layer to preserve as is. The \$a\$'th row [1, 1, 1, 1]
will be subtracted by the \$(a-1)\$'th row (would have been \$(a-2)\$'th row for odd dimension matrices); so [1-0, 1-8, 1-6, 1-4]
becomes [1, -7, -5, -3]
. The \$(a+1)\$'th row [1, 25, 0, 75]
will then be subtracted by the \$(a-2)\$'th row (would have been \$(a-3)\$'th row for odd dimension matrices); so [1-1, 25-3, 0-5, 75-7]
becomes [0, 22, -5, 68]
.
With folding option bottomright-to-topleft instead (with the same input-matrix above) we output the following as result:
[[-74, 2, 1, 7],
[ 0, 7, 6],
[-24, 1],
[ 1]]
With the following folding subtractions:
[[1-75, 3-1, 5-4, 7],
[ 0-0, 8-1, 6],
[1-25, 1],
[ 1]]
Challenge rules:
- You can use any eight distinct letters
[A-Za-z]
or distinct numbers in the range \$[-99,99]\$ for the folding options. Numbers \$[1..8]\$ or \$[0..7]\$ are probably the most common options, but if you want to use different numbers within the range for some smart calculations, feel free to do so. Please state which folding options you've used in your answer. - The input-matrix will always be a square NxN matrix, so you don't have to handle any rectangular NxM matrices. \$N\$ will also always be at least 2, since an empty or 1x1 matrix cannot be folded.
- The input of the matrix will always contain non-negative numbers in the range \$[0, 999]\$ (the numbers in the output will therefore be in the range \$[-999, 999]\$).
- With the (anti-)diagonal folding or odd-dimension vertical/horizontal folding, the middle 'layer' will remain unchanged.
- I/O is flexible. Can be a 2D array/list of integers; can be returned or printed as a space-and-newline delimited string; you can modify the input-matrix and replace the numbers that should be gone with
null
or a number outside of the[-999, 999]
range to indicate they're gone; etc. etc.
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 cases:
Input-matrix 1:
Input-matrix (for the following eight test cases):
[[ 1, 3, 5, 7],
[ 0, 8, 6, 4],
[ 1, 1, 1, 1],
[ 1,25, 0,75]]
Input-folding option: left-to-right
Output: [[2,6],[-2,4],[0,0],[-25,74]]
Input-folding option: right-to-left
Output: [[-6,-2],[-4,2],[0,0],[-74,25]]
Input-folding option: top-to-bottom
Output: [[1,-7,-5,-3],[0,22,-5,68]]
Input-folding option: bottom-to-top
Output: [[0,-22,5,-68],[-1,7,5,3]]
Input-folding option: topleft-to-bottomright
Output: [[7],[6,-1],[1,-7,-2],[1,24,0,74]]
Input-folding option: topright-to-bottomleft
Output: [[1],[-3,8],[-4,-5,1],[-6,21,-1,75]]
Input-folding option: bottomleft-to-topright
Output: [[1,3,4,6],[8,5,-21],[1,1],[75]]
Input-folding option: bottomright-to-topleft
Output: [[-74,2,1,7],[0,7,6],[-24,1],[1]]
Input-matrix 2:
Input-matrix (for the following eight test cases):
[[17, 4, 3],
[ 8, 1,11],
[11, 9, 7]]
Input-folding option: left-to-right
Output: [[4,-14],[1,3],[9,-4]]
Input-folding option: right-to-left
Output: [[14,4],[-3,1],[4,9]]
Input-folding option: top-to-bottom
Output: [[8,1,11],[-6,5,4]]
Input-folding option: bottom-to-top
Output: [[6,-5,-4],[8,1,11]]
Input-folding option: topleft-to-bottomright
Output: [[3],[1,7],[11,1,-10]]
Input-folding option: topright-to-bottomleft
Output: [[17],[4,1],[8,-2,7]]
Input-folding option: bottomleft-to-topright
Output: [[17,-4,-8],[1,2],[7]]
Input-folding option: bottomright-to-topleft
Output: [[10,-7,3],[-1,1],[11]]
A-Za-z
or any integer in the range[-999,999]
, so order doesn't matter. And sorry, but you must output the correct fold based on the input, so outputting all eight isn't allowed. \$\endgroup\$