Housie (#114)


Eric I. said it best in a note attached to his solution:

I really enjoyed this quiz. On the surface it seems relatively
But there are some important subtleties.

This is the realization I came to as well. When Brian first suggested
the quiz,
we discussed the possible solutions. When I say discussed, I mean that
generated a nearly endless stream of excellent ideas while I watched it
happen. The more he generated, the more I realized how much I liked the
problem. It’s quite rich.

There are many ways to approach this quiz. Brian’s own solution
generates bit
map patterns representing blanks and non-blanks in a ticket, then uses
patterns to construct books filling in numbers as it works. Eric I.
used a
backtracking search, verifying that a solution is still possible as each
is added. Below I will examine a slightly simpler solution, that still
pretty good results.

Andy Restrepo’s code is a random generation scheme. It builds random
books from
random tickets made out of random rows. If the book doesn’t end up
being valid,
it just tries again. However, it arranges the random generation such
that rows
are correct more often than you might expect.

Let’s jump into the code to see how it does that. The process begins
with this
trivial call:


That’s Ruby’s default new() and TicketGenerator doesn’t define an
method, so let’s skip to print_book():

class TicketGenerator
def print_book
# Keep generating ticket books until a valid
# one is returned. Then, print out the tickets.
book = build_book until book
book.each { |t| t.print_ticket; puts “\n”}

This is the process I explained earlier. An attempt is made to build a
If we get one, we’re done. Otherwise we try again. When we have them
all, the
tickets in the book are printed.

Let’s dig deeper into book creation:

class TicketGenerator
def build_book
# Generate 18 rows and divide them between six tickets
all_rows = Array.new(18){ retrieve_row }
tickets = Array.new
0.step(15, 3) do |x|
ticket = Ticket.new(all_rows[x…x+3].sort_by { rand })
# If an invalid ticket is found, indicate failure
# by setting the return value to false.
if not ticket.is_valid?
tickets = false; break

The first two lines here make use of other methods in the class to
create a set
of 18 rows. We will get to those methods in a moment, but take it on
faith for

The rest of the method walks those rows in threes, making Ticket objects
out of
them. Each time a Ticket is made, the code ensures it is valid or bails
out. We
haven’t seen this class yet either, but it shouldn’t too hard to guess
what the
is_valid?() method does.

Here are the row making methods (with one minor edit by me):

class TicketGenerator
def init_bins
# Create and fill the 9 bins of numbers, corresponding to
# the allowed numbers for each column.
@bins = Array.new
# 1 through 9
@bins << (1…9).sort_by{ rand }
# 10 through 19, 20 through 29, etc.
10.step(70, 10) do |x|
@bins << (x…x+9).sort_by{ rand }
# 80 through 90
@bins << (80…90).sort_by{ rand }
def retrieve_row
# Create a row by pulling one number from each of five non-empty
row = Array.new(9, nil)
# Randomize which bins to choose from, but favor the most filled
# bins – so we don’t end up with less than 5 non-empty bins with
# still more rows to create.
bin_index_array = (removed_email_address@domain.invalid).sort_by{ |b|
[@bins[b].length, rand]
5.times do
bin_index = bin_index_array.pop
row[bin_index] = @bins[bin_index].pop

First, init_bins() fills an Array with Arrays of randomized numbers for
column. These columns span six tickets and are used to build the entire

Now, retrieve_row() is used to create the 18 rows the code eventually
books out of. It works by creating an empty row, choosing five of the
bins, and adding a random number to the row from each bin. The
selection of
bins is not-quite-random and that turns out to be the best and worst
feature of
this solution.

Bins are sorted first by length and then in random order before a pick
is made
from the end. This means that a selection will always be made from a
bin with
the most members remaining. That bin is selected randomly from those
with the
same number of members remaining, but that doesn’t mean the choice is

Think of it this way, the bins don’t even start with the same number of
The eighth bin has eleven and will always sort to be chosen first.
the zeroth bin always sorts first and thus cannot be selected by this
during the creation of the first row. Even after the first row is
created the
eighth bin will still be tied for most entries and thus it will always
come up
again in the second row. That means the first ticket produced by this
will always have two or three numbers in the final column. Never just

The downside of all of this is that the tickets aren’t completely
There’s a pattern to them and you can find it if you look for it. Given
these books would be fine for casual play, but probably not for games
money is on the line.

The upside is speed. Even though tickets are randomly generated, the
keeps valid combination extremely likely. In a set of ten sample runs I
conducted the code only called build_book() a maximum of two times per
run and
it only needed one call eight out of ten times. A truly random pattern
need a lot more attempts to find a workable solution. You can simulate
scenario by removing the bin length sort condition from Andy’s code. It
finds answers, but it can take some time.

The last bit of TicketGenerator code sets method visibility:

class TicketGenerator
private :init_bins, :retrieve_row, :build_book
public :print_book

The other piece of this puzzle is the Ticket class, so we will look at
that now.
Tickets are just three rows that know how to validate and draw
themselves. The
initialization process for a Ticket is spread over three methods, so
let’s begin
with those:

class Ticket
def initialize(rows)
# A ticket consists of an array of three rows,
# with 5 numbers and 4 nil entries per row.
@rows = rows
@empty_column = false
def validate_ticket
# Convert three rows of 9 numbers into 9 columns of three numbers,
# check that each column satisfies the ascending order constraint,
# and then convert back into rows.
columns = Array.new(9) { [] }
columns.each { |c| @rows.each { |r| c << r.shift }; rectify© }
@rows.each { |r| columns.each { |c| r << c.shift } }
def rectify(column)
# If there are 2 or 3 numbers in a column, they must
# appear in increasing order downward. If they don’t, then
# swap the numbers around while maintaining 5 numbers
# in each row.
case column.nitems
when 0 then @empty_column = true
when 1 then column # do nothing
when 2
nil_index = column.index(nil)
non_nils = [0,1,2] - [nil_index]
first_nn, last_nn = non_nils.first, non_nils.last
# Swap the two non-nil elements
if column[first_nn] > column[last_nn]
column[first_nn], column[last_nn] = column[last_nn],
when 3 then column.sort! # just sort the three numbers

You can see that initialize() just stores the rows and kicks off
validate_ticket(). That method does a bit of a dance to rearrange the
rows into
columns, clean and validate those columns, and change them back.
Array#transpose() probably would have simplified this process a bit.

The real work happens in rectify(). This method serves two purposes.
Its main
job is to reorder the numbers in a column to ensure they count down. As
works though, it watches for empty columns that would invalidate the

Once we know whether or not a Ticket is valid, we need to provide easy
access to
that information:

class Ticket
def is_valid?
not @empty_column

That’s the method we saw TicketGenerator using to determine when a book
needed to be restarted.

Tickets can also print themselves:

class Ticket
def print_ticket
puts “±—” * 9 + “+”
@rows.each do |row|
line = row.inject("|") do |str, x|
if not x
str + " |"
elsif x < 10
str + " #{x} |"
str + " #{x} |"
puts line
puts “±—” * 9 + “+”

That code just walks the rows, printing fields as it goes. The if chain
in the
middle could be simplified a bit to:

str + " %2s |" % x

Finally, the code again sets the visibility for these methods:

class Ticket
private :validate_ticket, :rectify
public :print_ticket, :is_valid?

My thanks to all the book builders. I didn’t expect to see so many

Tomorrow, I will put you to work solving a long-standing Ruby Q.