This kata is inspired by a puzzle game for Android called Folding Blocks by Popcore Games. (video displayed below)
You are presented with a rectangular matrix (grid) containing some colored tiles [0]
All tiles of a given color form a single group, even if some tiles are separate from others.
A group can replicate by "folding" a copy of itself in a cardinal direction, namely up, down, left, or right. Refer to the reference image for additional clarity.
Your goal is to write a function that returns a sequence of folds to completely fill a given grid with colored tiles [9]
NOTE that a fold can only occur if the positions of all the newly created cells are empty and remain within the confines of the grid.
Your function will receive a single string as an argument. The string will consist of any combination of uppercase alphabetic letters, spaces, and newline characters. Their representations are as follows:- Each unique alphabetic letter represents its own unique color.
- Spaces represent an empty tile space in the grid
- Newline characters represent the delimiters between rows (and all rows are of equal length)
If there is no possible solution, return null
or None
.
A graphic illustration of this test example is provided in the section Mechanics Overview, above. In our example solution, the first fold is given as BL
, meaning that the group of tiles of color B
will fold to the Left
, resulting in frame [1]
in the illustration above (the new tiles created from the fold are displayed with thick white borders)
example = '\n'.join((
'AAA ',
' A ',
' B B',
' ',
' C D ',
' '
))
# This is just one of multiple valid solutions
solver(example)# ('BL','BU','AR','AD','CR','CL','CD','DD','DR')
const example = [
'AAA ',
' A ',
' B B',
' ',
' C D ',
' '
].join('\n');
// This is just one of multiple valid solutions
solver(example)// ['BL','BU','AR','AD','CR','CL','CD','DD','DR']
- The maximum number of unique colors for any given test is
6
- The vertical and horizontal lengths of the grid are in the range:
2 <= length <= 12
- Test cases will have
0
or more possible solutions - Full Test Suite:
16
fixed tests and46
random tests - Use Python 3+ for the Python translation
- All inputs will be valid
If you enjoyed this kata, be sure to check out my other katas