Input:
- An integer
n
- Two equal-sized square matrices (with their width/height being a multiple of
n
)
Output:
One of two distinct values of your own choice, one being for truthy results and one for falsey results (so yes, 1/0
instead of true/false
are valid outputs for languages like Java, even though they're not considered official truthy/falsey values).
The truthy/falsey output indicates whether we can rearrange blocks of size n by n
in one matrix to make it equal to the other matrix.
Example:
Input:
Matrix 1:
1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 8 7 6 5 4
3 2 1 0 9 8
1 1 1 1 1 1
Matrix 2:
3 2 9 8 7 8
1 1 1 1 5 4
3 4 5 6 1 0
9 0 7 6 1 1
5 6 1 2 3 4
1 2 7 8 9 8
Integer n:
2
Output: truthy
Why?
If we split the matrices in blocks of 2 by 2
, we can see that all blocks on one matrix can also be found in the other matrix:
Matrix 1:
1 2 | 3 4 | 5 6
7 8 | 9 0 | 1 2
---------------
3 4 | 5 6 | 7 8
9 8 | 7 6 | 5 4
---------------
3 2 | 1 0 | 9 8
1 1 | 1 1 | 1 1
Matrix 2:
3 2 | 9 8 | 7 8
1 1 | 1 1 | 5 4
---------------
3 4 | 5 6 | 1 0
9 0 | 7 6 | 1 1
---------------
5 6 | 1 2 | 3 4
1 2 | 7 8 | 9 8
Challenge rules:
- You can assume the matrices will only contain non-negative digits (range
[0,9]
) - You can assume the width/height of the matrices are equal, and a multiple of
n
- You can assume
n
will be in the range[1, 50]
, and the width/height of the matrices are in the range[1,100]
. - The individual blocks of
n by n
can only be used once to determine if the matrices are permutations of each other when split into blocks ofn by n
. - There can be multiple
n by n
blocks that are the same. - The
n by n
blocks will remain in the same orientation when checking if the two matrices are permutation of each other when split into blocks ofn by n
.
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: Matrix 2: Integer:
1 2 3 4 5 6 3 2 9 8 7 8 2
7 8 9 0 1 2 1 1 1 1 5 4
3 4 5 6 7 8 3 4 5 6 1 0
9 8 7 6 5 4 9 0 7 6 1 1
3 2 1 0 9 8 5 6 1 2 3 4
1 1 1 1 1 1 1 2 7 8 9 8
Output:
truthy
Input:
Matrix 1: Matrix 2: Integer:
1 2 3 4 5 6 3 2 9 8 7 8 1
7 8 9 0 1 2 1 1 1 1 5 4
3 4 5 6 7 8 3 4 5 6 1 0
9 8 7 6 5 4 9 0 7 6 1 1
3 2 1 0 9 8 5 6 1 2 3 4
1 1 1 1 1 1 1 2 7 8 9 8
Output:
truthy
Input:
Matrix 1: Matrix 2: Integer:
1 2 3 4 5 6 3 2 9 8 7 8 3
7 8 9 0 1 2 1 1 1 1 5 4
3 4 5 6 7 8 3 4 5 6 1 0
9 8 7 6 5 4 9 0 7 6 1 1
3 2 1 0 9 8 5 6 1 2 3 4
1 1 1 1 1 1 1 2 7 8 9 8
Output:
falsey
Input:
Matrix 1: Matrix 2: Integer:
1 2 3 4 1 2 3 4 4
2 3 4 5 2 3 4 5
3 4 5 6 3 4 5 6
4 5 6 7 4 5 6 7
Output:
truthy
Input:
Matrix 1: Matrix 2: Integer:
1 2 3 4 3 4 3 4 2
2 3 4 5 4 5 4 5
3 4 5 6 1 2 5 6
4 5 6 7 2 3 6 6
Output:
falsey
Input:
Matrix 1: Matrix 2: Integer:
1 2 2 3 1
3 4 1 1
Output:
falsey
Input:
Matrix 1: Matrix 2: Integer:
0 8 1
Output:
falsey
Input:
Matrix 1: Matrix 2: Integer:
1 2 3 4 1 2 1 2 2
5 6 7 8 5 6 5 6
9 0 0 9 0 9 9 0
4 3 2 1 2 1 4 3
Output:
falsey
Input:
Matrix 1: Matrix 2: Integer:
1 2 1 2 9 5 1 2 2
3 4 3 4 7 7 3 4
8 3 9 5 1 2 8 3
6 1 7 7 3 4 6 1
Output:
truthy
Input:
Matrix 1: Matrix 2: Integer:
1 0 2 0 0 3 1 1 1 0 0 3 2
1 1 1 1 1 1 2 0 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5
Output:
falsey
[ [ 0 ] ], [ [ 25 ] ], 1
present? I understood withYou can assume the matrices will only contain non-negative digits (range [0,9])
that the matrix values are only between 0 and 9? \$\endgroup\$[0,9]
later on in the Sandbox. I've changed the test case to[[0]],[[8]]
. \$\endgroup\$