The purpose of this challenge is to determine if a collection of one-dimensonal pieces can be tiled to form a finite continuous chunk.
A piece is a non-empty, finite sequence of zeros and ones that starts and ends with a one. Some possible pieces are 1
, 101
, 1111
, 1100101
.
Tiling means arranging the pieces so that a single contiguous block of ones is formed. A one from one piece can occupy the place of a zero, but not of a one, from another piece.
Equivalently, if we view a one as being "solid material" and a zero as a "hole", the pieces should fit so as to form a single stretch, without leaving any holes.
To form a tiling, pieces can only be shifted along their one-dimensional space. (They cannot be split, or reflected). Each piece is used exactly once.
Examples
The three pieces 101
, 11
, 101
can be tiled as shown in the following, where each piece is represented with the required shift:
10111 101
so the obtained tiling is
111111
As a second example, the pieces 11011
and 1001101
cannot be tiled. In particular, the shift
110111001101
is not valid because there are two ones that collide; and
11011 1001101
is not valid because the result would contain a zero.
Additional rules
The input is a collection of one or more pieces. Any reasonable format is allowed; for example:
- A list of strings, where each string can contain two different, consistent characters;
- Several arrays, where each array contains the positions of ones for a piece;
- A list of (odd) integers such the binary representation of each number defines a piece.
The output should be a truthy value if a tiling is possible, and a falsy value otherwise. Output values need not be consistent; that is, they can be different for different inputs.
Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
Shortest code in bytes wins.
Test cases
Each input is on a different line
Truthy
11111, 111, 111, 1111101, 11, 1101, 11, 10110001, 11001, 10001100001, 1001, 101110010001, 1001, 1001, 10110110101, 11001, 100001, 1110111, 100001, 11, 1011001101, 110111, 1, 11, 1
Falsy
101101, 111, 10011011, 101111011, 10011011001, 11011, 10000011001, 11011, 1000001, 10101