A while ago, I posted a challenge asking to determine whether or not it's possible to arrange arbitrary rectangles to fill a rectangular space, here. That got answers, so clearly it was too easy. (Just kidding, it was fairly challenging, congrats to the answerers :D)
Challenge
Given a bunch of arbitrary shapes (consisting of 1x1 square tiles) (up to 100 with a combined area of at most 1000), can you arrange them to fill a rectangular space, without rotations or reflections?
A shape is valid as input if it has at least 1 tile and all tiles are connected to each other by edges. For example:
Valid:
XXX
X X
XXX
Invalid:
XXX
X
XX
The shapes will be represented in input by using a non-whitespace character X
, spaces, and newlines. Individual shapes will be separated by two newlines (i.e. split on \n\n
). You can choose to use LF, CR, or CRLF as a newline. Alternatively, you can choose to use LF or CR as a single newline and CRLF as a double-newline for separating shapes. Please specify in your answer what you choose.
For example, this would be the input format for a 2x2 square and a 3x3 "donut":
XX
XX
XXX
X X
XXX
(Optional trailing newline or double-newline)
The output should be either any truthy value or a consistent falsy value (but not an error). For example, you may output the valid arrangement if one exists or false, but you cannot error if there is no valid arrangement, neither can you output different values for having no valid arrangement.
Test Cases
Test Case 1
XXXX
X X
XX X
X
X
X
TRUE
Valid Arrangement:
XXXX
XYYX
XXYX
Test Case 2
XXXX
XX
X
FALSE
Test Case 3
XXXXXX
X X X
X XXX
XX
X
X
X
TRUE
Valid Arrangement:
XXXXXX
XYXZZX
XYXXXA
Test Case 4
XX
X
X
XX
XXX
X
X
X
TRUE
Valid Arrangement:
XXYZ
XYYZ
AAAZ
Notes
- Each shape must be used exactly once in the arrangement
- Shapes may not be rotated or reflected
- The input can contain multiple identical shapes; in this case, each of those still needs to be used exactly once; that is, if two 5x3 rectangles are given in the input, two 5x3 rectangles must appear in the arrangement