Problem 42708. Placing Beads Neatly in a Box
You are given a string of n black and white beads. Your job is to pack them neatly into a square box. "Neatly" in this case means that all the black beads are at the bottom, and all the white beads are at the top.
Half the beads are black, and half are white. The number of beads n will always be an even number perfect square (4, 16, 36, ...). Black beads are 1, and white beads are 0, so a string might look like this.
Return a square matrix bx that indexes into str such that
The matrix bx consists of the numbers 1 through n snaking through the box in a 4-connected sense (see Cody Problem 42705, Is It a Snake?).
Here's one solution for the string shown above.
In general the answers are not unique. I will be checking that bx contains the numbers 1 through n, that they form a snake, and that when used with the string of beads, they result in a tidy ones-on-the-bottom formation.
I am grateful to the solvers of problem 42705 for giving me nice short code to use in my test suite for this problem!
Solution CommentsShow comments
Problem Recent Solvers13