Matrix Jigsaw Puzzles
APL (Dyalog Extended), 19 18 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
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