Since I posed the problem in the first place, here’s my solution.
As a bit of background: after submitting the first draft of the quiz to
James, I made it an explicit requirement that numbers had to be placed
column in increasing order downwards, e.g.
| 85 | | 86 |
| | is OK, but | | is not.
| 86 | | 85 |
A little thought shows that it’s always possible to transform a “bad”
solution like the RHS into a “good” solution like the LHS, just by
the values around, keeping any blanks in the same position.
This in turn led me to realise that the only thing which matters is the
position of blanks and non-blanks, and this can be represented in a
The attached solution enumerates all possible tickets grids up-front,
is remarkably quick when using a Fixnum to represent each bitmap.
Then, assembling together 6 grids to form a book is a case of picking 6
grids which have a total of 9 non-blanks across the first column, 10
non-blanks across the second…eighth columns, and 11 non-blanks across
last column. I couldn’t think of a deterministic way of doing this, so
just picks 5 grids at random and looks in an index to find if there is
6th grid which could be used to complete the book.
After implementing the solution, I realised that the three rows in a
can be shuffled around in any order without breaking the structure of
the ticket or the book. This could be used to reduce the number of
grid patterns by a factor of 6; then when you generate a book, as a last
step you can randomly shuffle the rows in each ticket.
P.S. Note to James: I just discovered a silly bug in
which has been fixed in the attached version