# Matrix Jigsaw Puzzles

### 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 of n 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 of n by n.

## General rules:

• This is , 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.

## 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


Pastebin with matrices in [[,]] format.

• Can we take the matrices as a list of matrices?
– Jo King
Jan 14, 2019 at 9:46
• @JoKing You mean a list with both matrices instead of two separated matrix-inputs? If that's what you're asking, then sure, why not. Jan 14, 2019 at 9:47
• Jan 14, 2019 at 11:15
• Why is the example [ [ 0 ] ], [ [ 25 ] ], 1 present? I understood with You can assume the matrices will only contain non-negative digits (range [0,9]) that the matrix values are only between 0 and 9? Jan 14, 2019 at 12:59
• @OlivierGrégoire Sorry, added that rule about range [0,9] later on in the Sandbox. I've changed the test case to [],[]. Jan 14, 2019 at 13:01

# APL (Dyalog Extended), 1918 17 bytes

-2 thanks to ngn.

Anonymous tacit infix function. Takes n as left argument and list of two matrices as right argument. Requires zero-indexing (⎕IO←0). Incidentally, this function works on arrays of any number of dimensions.

≡.{∧,⍵⊂⍨⊂0=⍺|⍳≢⍵}


Try it online!

≡.{} identical results of the following function applied to each matrix ⍵ with n as ⍺?

≢⍵ size of matrix

⍳ indices 0…size–1

⍺| division remainder when divided by n

⊂ enclose to use along all dimensions

⍵⊂⍨ use that to partition* the matrix into a matrix of submatrices
* begins new partition when corresponding element is less than the previous; removes elements marked by zero

, ravel the matrix into a list of submatrices

∧ sort ascending

• (≢⍵)⍴⍺↑1 -> 0=⍺|⍳≢⍵ (with ⎕io←0)
– ngn
Jan 14, 2019 at 16:23
• @ngn Thanks. Done.
Jan 14, 2019 at 21:56
• ≡/{}¨ -> ≡.{}
– ngn
Jan 31, 2019 at 13:25
• @ngn Done. Thanks.
Jan 31, 2019 at 15:38

# Python 2, 108 103 bytes

lambda s,*a:len(set(sorted(sum([zip(*[iter(zip(*l))]*s)for l in zip(*[iter(m)]*s)],[]))for m in a))<2


Try it online!

# Perl 6, 94 68 63 bytes

{[eqv] map *.rotor($^a).map({[Z]$_}).flat.rotor($a²).sort,@_}  Try it online! Anonymous code block that takes input as size, [matrix1, matrix2] and returns a boolean True/False. There might be a more efficient way of splitting the matrix into chunks than rotor. ### Explanation: { } # Anonymous code block map ,@_ # For both matrices *.rotor($^a)   # Split the matrix into N sized chunks
.map({[Z] $_}) # Then zip each of those chunks together .flat # Flatten the resulting list .rotor($a²)  # Then split into the NxN lists
.sort   # And sort them
[eqv]    # And then check if the lists are equivalent


# 05AB1E, 14 bytes

εε²ô}ø˜²ô²ô{]Ë


Try it online!

# Jelly,  10  9 bytes

ż⁹/ẎsṢʋ€E


Try it online! (or with pre-processing for easier copy & paste from the test cases)

A dyadic Link accepting a list of the two matrices (as lists of lists) on the left and the integer on the right which yields 1 or 0 for truthy or falsey respectively.

### How?

ż⁹/ẎsṢʋ€E - Link: [M1, M2]; N
€  - for each of [M1, M2]:
⁹        -     (chain's right argument, N)
⁹/       -     N-wise-reduce with:
ż         -       zip together
Ẏ      -     tighten
s     -     split into chunks of length N
Ṣ    -     sort
E - equal?


# Japt, 18 bytes

®mòV yòV rc n qÃr¥


Try it online!

Explanation:

®              Ã      #Apply this to each of the input matrices:
mòV                  # Split each row into groups of n
yòV              # Split each column into groups of n
rc           # Flatten into a list of nxn submatrices
n         # Sort that list
q       # Turn it into a string
r¥    #Return true if both matrices had identical results


The "Turn it into a string" step is necessary because Japt doesn't compare arrays by value and the builtin to work around that doesn't work for multidimensional arrays.

• I'll see if I can make some time between meetings tomorrow to try and get A.e() working for multi-dimensional arrays; always meant to come back to it. In the meantime ÕmòV -> yòV will save you a byte. Jan 14, 2019 at 21:30
• By the way, the limitation on comparing arrays for equality is JavaScript's rather than being particular to Japt ;) Jan 15, 2019 at 12:57

# TSQL, 164 bytes

Populating a table variable in order to have input, this creating input and inserting data has not been included in the byte count. Only the actual query to extract the data.

Golfed(not including test table - it can be found in the ungolfed version):

SELECT iif(exists(SELECT*FROM(SELECT string_agg(v,'')within
group(order by x,y)s,m FROM @t GROUP BY x/@,y/@,m)x
GROUP BY s HAVING max(m)=min(m)or sum(m-.5)<>0),0,1)


Ungolfed:

-- test data
DECLARE @ INT = 2
-- x = x-position of the input
-- y = y-position of the input
-- v = value
-- m = matrix(0 or 1)
DECLARE @t table(x int, y int, v int, m int)
--insert first matrix values
INSERT @t values
(0,0,1,0),(0,1,2,0),(0,2,1,0),(0,3,2,0),
(1,0,3,0),(1,1,4,0),(1,2,3,0),(1,3,4,0),
(2,0,8,0),(2,1,3,0),(2,2,9,0),(2,3,5,0),
(3,0,6,0),(3,1,1,0),(3,2,7,0),(3,3,7,0)
INSERT @t values
(0,0,9,1),(0,1,5,1),(0,2,1,1),(0,3,2,1),
(1,0,7,1),(1,1,7,1),(1,2,3,1),(1,3,4,1),
(2,0,1,1),(2,1,2,1),(2,2,8,1),(2,3,3,1),
(3,0,3,1),(3,1,4,1),(3,2,6,1),(3,3,1,1)

-- query
SELECT iif(exists
(
SELECT *
FROM
(
SELECT string_agg(v,'')within group(order by x,y)s,m
FROM @t
GROUP BY x/@,y/@,m
) x
GROUP BY s
HAVING max(m)=min(m)or sum(m-.5)<>0
),0,1)


Try it out

# JavaScript (ES6), 88 bytes

(n,a,b)=>(g=a=>a.map((r,y)=>r.map((v,x)=>o[y/n<<7|x/n]+=[v]),o=[])&&o.sort()+o)(a)==g(b)


Try it online!

### How?

This code is:

• extracting all sub-matrices in each input matrix as a concatenation of cells
• sorting the sub-matrices in lexicographical order
• testing whether the result is the same for both input matrices

It is taking advantage of the limits described in the challenge:

• A matrix consists of single digits, so we can just concatenate all cells of a sub-matrix without any separator and still get a unique representation of it (e.g. [[1,2],[3,4]] can be stored as "1234").

• The width of the input matrices is less than or equal to $$\100\$$. To convert the coordinates $$\(x,y)\$$ in an input matrix into a unique slot index $$\I\$$ in our storage area, we can do:

$$I=\left\lfloor\frac{y}{n}\right\rfloor\times128+\left\lfloor\frac{x}{n}\right\rfloor$$

or as JS code: y / n << 7 | x << n

### Commented

(n, a, b) =>           // n, a, b = input variables (integer, matrix 1, matrix 2)
(g = a =>            // g = helper function taking one of the two matrices
a.map((r, y) =>    // for each row r[] at position y in a[]:
r.map((v, x) =>  //   for each value v at position x in r[]:
o[             //     update o[]:
y / n << 7 | //       the position of the slot is computed by taking advantage
x / n        //       of the limit on the matrix width (see above)
] += [v]       //     coerce v to a string and append it to o[slot]
//     all slots are initially undefined, so all resulting strings
),               //   end of inner map()
) &&               // end of outer map()
o.sort() + o       // sort o[] and coerce it to a string by concatenating it with itself
)(a) == g(b)         // test whether g(a) is equal to g(b)


# Java (JDK), 217 bytes

(n,a,b)->{java.util.Arrays A=null;int l=a.length,x=l/n,j=0,z;var c=new String[x*x];A.fill(c,"");var d=c.clone();for(;j<l*l;d[z]+=b[j/l][j++%l])c[z=j/l/n+j%l/n*x]+=a[j/l][j%l];A.sort(c);A.sort(d);return A.equals(c,d);}


Try it online!

## Explanation

The idea is to pick each small cell as a string, which is comparable, and then to sort those strings and compare them in order.

(n,a,b)->{
java.util.Arrays A=null;             // Shortcut A for the several java.util.Arrays that'll come
int l=a.length,x=l/n,i=0,j,z;        // Variable declarations
var c=new String[x*x];               // Declare the small squares list
A.fill(c,"");                        // Fill the lists of small squares with the empty string.
var d=c.clone();                     // Make a copy of the list, for the second matrix
for(;i<l;i++)
for(j=0;j<l;d[z]+=b[i][j++])        // For each matrix cell
c[z=i/n+j/n*x]+=a[i][j];           // Fill the small square with the value, string-wise
A.sort(c);A.sort(d);                 // Sort both small squares list
return A.equals(c,d);                // Return true if they're equal, false otherwise.
}


Note: some lines are obsolete.

## Credits

• -12 bytes thanks to Kevin Cruijssen!
• -4 bytes thanks to ceilingcat.
• Did you forgot to golf for(j=0;j<l;){c[z=i/n+j/n*x]+=a[i][j];d[z]+=b[i][j++];}?.. You can remove the brackets by putting everything inside the loop. Also, the i=0 in the loop can be removed, because your i is already 0 at declaration. Jan 14, 2019 at 13:28
• And one thing to actually golf: var d=new String[x*x]; can be var d=c.clone(); instead. 234 bytes Jan 14, 2019 at 13:29
• PS: Why does your TIO contains String which you convert to 2D-integer arrays? I've added a paste-bin with the test cases at the bottom, for which you can replace the [ and ] with { and } and add a leading new int[][], and it would have been enough. ;) Jan 14, 2019 at 13:31
• Dammit, I hadn't seen the paste-bin :( And for the rest, I'm still golfing. I did a rough pass, but thank you :-) Jan 14, 2019 at 13:33
• The i=0 was a remnant when I filled the arrays by myself rather than using Arrays.fill. Thanks :-) And for the clone I thought about using it, but I still thought it'd have returned anObject and not the actual type. I must be several versions late on that point ;) Jan 14, 2019 at 13:36

# Charcoal, 54 49 bytes

1ＦθＦ⪪ιηＦ÷Ｌ§κ⁰η⊞υＥκ§⪪μηλＷ∧υ⊟υ¿№✂υ⁰⊘⊕Ｌυ¹ι≔Φυ⁻⌕υιλυ⎚


Try it online! Link is to verbose version of code. Takes input as an array of equal-sized two-dimensional arrays. Outputs 1 on success, nothing on failure. Explanation:

1


Assume success.

Ｆθ


Loop over the arrays.

Ｆ⪪ιη


Divide the array into n-sized row chunks.

Ｆ÷Ｌ§κ⁰η


Loop over each column chunk.

⊞υＥκ§⪪μηλ


Extract the column chunk for each row of the row chunk and save the resulting submatrix in a list.

Ｗ∧υ⊟υ


While the list is nonempty, remove the last chunk of the list, which under normal circumstances comes from the second array.

¿№✂υ⁰⊘⊕Ｌυ¹ι


Count the number of occurrences of that chunk in the first half of the list, which under normal circumstances contains the remaining chunks from the first array.

≔Φυ⁻⌕υιλυ


If nonzero then remove the first occurrence of that chunk from the list.

⎚


If zero then clear the output, making it falsy.

# J, 55 bytes

[:-:/[([:/:~([*-@[)]\,@])"3(((;])@(#@]$1{.~[),;.1])&>])  Try it online! A horrible solution, just made it work - I have no power to golf it... • 29 bytes Jul 19, 2022 at 15:55 • @Jonah Thanks! Feel free to post it separately. Jul 20, 2022 at 7:46 ## Haskell, 74 73 bytes import Data.Lists i#m|c<-chunksOf i=c.transpose=<<c m (m!n)i=i#m\\i#n==[]  Note: TIO hasn't installed Data.Lists, so I'm using Data.List instead an add the missing function chunksOf: Try it online! i#m= -- function '#' makes a list of all transposed jigsaw blocks of matrix 'm' -- of size 'i' c<-chunksOf i -- define helper function 'c' that splits it's argument into -- chunks of site 'i' c m -- split the matrix into chunks of size 'i' =<< -- for each chunk transpose -- transpose c. -- and split into chunks of size 'i', again -- flatten one level of nesting ('=<<' is concatMap) (m!n)i= -- main function i#m\\i#n -- remove every element of i#n from i#m ==[] -- and check if it results in an empty list  # C# (Visual C# Interactive Compiler), 178 bytes (a,b,n)=>{string[]s(int[][]c){int i=0,l=a.Length,m=l/n;var r=new string[m*m];for(;i<l*l;)r[i/l/n*m+i%l/n]+=c[i/l][i++%l];Array.Sort(r);return r;}return s(a).SequenceEqual(s(b));}  Try it online! -1 thanks to @KevinCruijssen! -8 thanks to @ceilingcat! Less golfed code: // anonymous function // a and b are 2d jagged arrays // n is the size of the sub matrix (a,b,n)=>{ // helper function that translates // the sub matrices into strings // of digits. string[]s(int[][]c){ // i and j are loop counters int i=0,j, // l is the size of a side of a matrix l=a.Length, // m is the number of sub matrices // per side of a matrix m=l/n; // the concatenated digits are // stored in a single dimension // array var r=new string[m*m]; // flattened loop builds up // the digit strings for(;i<l*l;) r[i/l/n*m+i%l/n]+=c[i/l][i++%l]; // The resulting array is // sorted before it is returned for // ease of comparison. Array.Sort(r); return r; } return s(a).SequenceEqual(s(b)); }  • One minor thing to golf, the j++ can be removed and can be placed at +=c[i][j++]+" "; to save a byte. Jan 15, 2019 at 6:57 • Thanks for the tip :) Interestingly enough, I came up with almost the exact same solution as the Java one. – dana Jan 15, 2019 at 7:02 # PHP, 186163 162 bytes function($a,$b,$n){$f=function($j,$n){foreach($j as$x=>$r)foreach($r as$y=>$v)$o[count($j)*($x/$n|0)+$y/$n|0].=$v;sort($o);return$o;};return$f($a,$n)==$f($b,$n);}


Try it online!

Like all good challenges, I started off thinking this was fairly easy and it threw me some curves. Nicely done @Kevin Cruijssen!

Chunks the matrix into strings containing the values for each block. Arrays are then sorted and compared for equality.

Ungolfed:

function jigsaw_chunk( $j,$n ) {
foreach( $j as$x => $r ) { foreach($r as $y =>$v ) {
$o[ count($j ) * floor( $x/$n ) + floor( $y/$n )] .= $v; } } sort($o );
return $o; } function jigsaw_test($a, $b,$n ) {
return jigsaw_chunk( $a,$n ) == jigsaw_chunk( $b,$n );
}

// Test 6
var_dump( jigsaw_test( [[1,2],[3,4]], [[2,3],[1,1]], 1 ) );


Output

bool(false)


# Red, 148147 142 bytes

func[a b n][g: func[m][
sort collect[loop k:(length? m)/ n[i: 0
loop k[keep/only
collect[loop n[keep
take/part m/(i: i + 1) n]]]]]](g a)= g b]


Try it online!