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
nwill 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 ncan 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 nblocks that are the same. - The
n by nblocks 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
15 Answers 15
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=⍺|⍳≢⍵}
≡.{...} 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
-
\$\begingroup\$
(≢⍵)⍴⍺↑1->0=⍺|⍳≢⍵(with⎕io←0) \$\endgroup\$ngn– ngn2019年01月14日 16:23:17 +00:00Commented Jan 14, 2019 at 16:23 -
\$\begingroup\$ @ngn Thanks. Done. \$\endgroup\$Adám– Adám2019年01月14日 21:56:12 +00:00Commented Jan 14, 2019 at 21:56
-
\$\begingroup\$
≡/{}¨->≡.{}\$\endgroup\$ngn– ngn2019年01月31日 13:25:31 +00:00Commented Jan 31, 2019 at 13:25 -
\$\begingroup\$ @ngn Done. Thanks. \$\endgroup\$Adám– Adám2019年01月31日 15:38:04 +00:00Commented Jan 31, 2019 at 15:38
Perl 6, (削除) 94 68 (削除ここまで) 63 bytes
{[eqv] map *.rotor($^a).map({[Z] $_}).flat.rotor($a2).sort,@_}
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($a2) # Then split into the NxN lists
.sort # And sort them
[eqv] # And then check if the lists are equivalent
Jelly, (削除) 10 (削除ここまで) 9 bytes
ż9/Ẏ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?
ż9/ẎsṢʋ€E - Link: [M1, M2]; N
€ - for each of [M1, M2]:
ʋ - last four links as a dyad (i.e. f(M, N)):
9 - (chain's right argument, N)
9/ - 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\
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.
-
2\$\begingroup\$ 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òVwill save you a byte. \$\endgroup\$Shaggy– Shaggy2019年01月14日 21:30:42 +00:00Commented Jan 14, 2019 at 21:30 -
\$\begingroup\$ By the way, the limitation on comparing arrays for equality is JavaScript's rather than being particular to Japt ;) \$\endgroup\$Shaggy– Shaggy2019年01月15日 12:57:37 +00:00Commented 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)
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)
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
// are going to start with "undefined", which is harmless
), // end of inner map()
o = [] // start with o = empty array
) && // 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);}
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.
-
\$\begingroup\$ 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, thei=0in the loop can be removed, because youriis already 0 at declaration. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年01月14日 13:28:10 +00:00Commented Jan 14, 2019 at 13:28 -
\$\begingroup\$ And one thing to actually golf:
var d=new String[x*x];can bevar d=c.clone();instead. 234 bytes \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年01月14日 13:29:10 +00:00Commented Jan 14, 2019 at 13:29 -
\$\begingroup\$ 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 leadingnew int[][], and it would have been enough. ;) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年01月14日 13:31:27 +00:00Commented Jan 14, 2019 at 13:31 -
\$\begingroup\$ Dammit, I hadn't seen the paste-bin :( And for the rest, I'm still golfing. I did a rough pass, but thank you :-) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2019年01月14日 13:33:06 +00:00Commented Jan 14, 2019 at 13:33
-
\$\begingroup\$ The
i=0was a remnant when I filled the arrays by myself rather than usingArrays.fill. Thanks :-) And for thecloneI thought about using it, but I still thought it'd have returned anObjectand not the actual type. I must be several versions late on that point ;) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2019年01月14日 13:36:36 +00:00Commented Jan 14, 2019 at 13:36
Charcoal, (削除) 54 (削除ここまで) 49 bytes
1FθF⪪ιηF÷L§κ0η⊞υEκ§⪪μηλW∧υ⊟υ¿No✂υ0⊘⊕Lυ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.
Fθ
Loop over the arrays.
F⪪ιη
Divide the array into n-sized row chunks.
F÷L§κ0η
Loop over each column chunk.
⊞υEκ§⪪μηλ
Extract the column chunk for each row of the row chunk and save the resulting submatrix in a list.
W∧υ⊟υ
While the list is nonempty, remove the last chunk of the list, which under normal circumstances comes from the second array.
¿No✂υ0⊘⊕Lυ1ι
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])&>])
A horrible solution, just made it work - I have no power to golf it...
-
1
-
1\$\begingroup\$ @Jonah Thanks! Feel free to post it separately. \$\endgroup\$Galen Ivanov– Galen Ivanov2022年07月20日 07:46:39 +00:00Commented 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));}
-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));
}
-
\$\begingroup\$ One minor thing to golf, the
j++can be removed and can be placed at+=c[i][j++]+" ";to save a byte. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年01月15日 06:57:47 +00:00Commented Jan 15, 2019 at 6:57 -
\$\begingroup\$ Thanks for the tip :) Interestingly enough, I came up with almost the exact same solution as the Java one. \$\endgroup\$dana– dana2019年01月15日 07:02:13 +00:00Commented Jan 15, 2019 at 7:02
PHP, (削除) 186 (削除ここまで) (削除) 163 (削除ここまで) 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);}
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, (削除) 148 (削除ここまで) (削除) 147 (削除ここまで) 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]
[ [ 0 ] ], [ [ 25 ] ], 1present? 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\$