Consider a non-empty binary matrix M and a natural number n. For the purposes of this challenge, M is said to have blockiness n if it can be built using adjacent square blocks of size n, where each block has equal entries; and it cannot be formed using square blocks of any larger size. Intuitively, n can be thought of as the "pixel size" of the matrix.
Example 1: let the values of the matrix be represented as +
and o
for clarity.
++oo++++
++oo++++
oo++++++
oo++++++
has blockiness 2
. Although some entries can be considered to belong to larger blocks, 2
is the maximum block size that is valid for all entries. To be specific, the blocks are shown below, using ·
as separator:
++·oo·++·++
++·oo·++·++
···········
oo·++·++·++
oo·++·++·++
Example 2:
+++oo+++
+++oo+++
has blockiness 1
. Even if any entry can be seen as belonging to some "sliding" block of size 2
, it is not possible to form the matrix using adjacent blocks of that size.
The challenge
Given a non-empty binary matrix, output its blockiness.
Rules
- Any two consistent values can be chosen to define the matrix. Input format is flexible as usual (2D array, list of lists of numbers, flattened array and one or two numbers defining its shape, list of strings, ...).
- Input and output means are flexible as usual. Programs or functions are allowed. Standard loopholes are forbidden.
- Code golf, shortest wins.
Test cases
See also "inputs in common formats" at the end.
- Blockiness
1
:
+
ooo
+o
++
+++oo+++
+++oo+++
ooo+++
ooo+++
++++++
++oooo
ooooo
ooooo
ooooo
ooooo
- Blockiness
2
:
oo
oo
++++
++++
++oo++++
++oo++++
++oo++++
++oo++++
oo++++++
oo++++++
- Blockiness
3
:
++++++ooo
++++++ooo
++++++ooo
ooo+++ooo
ooo+++ooo
ooo+++ooo
+++ooo+++
+++ooo+++
+++ooo+++
- Blockiness
4
:
++++++++
++++++++
++++++++
++++++++
++++++++oooo
++++++++oooo
++++++++oooo
++++++++oooo
++++++++oooo
++++++++oooo
++++++++oooo
++++++++oooo
Inputs in common formats:
- Numerical matrix, Matlab/Octave
- Character matrix, Matlab/Octave
- Nested list, Python
- List of strings, Python
- Flattened array (row-major order) and shape (numbers of rows and columns)
- If you think that some common format is missing, leave a comment and I'll try to generate it automatically.