Forum: Ruby Verbal Arithmetic (#128)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-06-21 14:51
(Received via mailing list)
I'm always impressed by the creativity of the solutions, but I think
this week
stands out even more than usual.  I literally spent hours going through
solutions and learned some really great tricks from them.  I wish I
could take
you on the same tour of the code, but that would take this summary into
range of a full text book in length.

Because I'm going to miss all of the following, let me point out some
for your own explorations:

  * Though the brute-force solutions are slow, most of them handle any
    math equations Ruby can.  That is an interesting advantage.
  * Andreas Launila sent in a fun preview of his Google Summer of Code
    project that looks to simplify many of these search problems we
    commonly use as quizzes.
  * Glen's solution is a nifty metaprogramming solution that customizes
    itself to the equation entered.  It's lightning quick too.
  * Morton Goldberg solved the quiz with some genetic programming and
    that code is still quite a bit zippier than a brute-force search.

The solution I will show is from Eric I.  It has an interesting state
design that tries to fail fast in an attempt to aggressively prune the
space.  It too finds solutions quite rapidly, though it only works for

Eric's code breaks the equation down into a small series of steps.
Instead of
searching for a match for all numbers and then checking the result, this
solution checks as many little sub-criteria as possible.  Does just this
add up correctly, given what we know at this point?  Is this digit a
because it starts a term somewhere else?

These smaller tests lead to failures that allow the search to skip large
in the set of possible solutions. For example, if S can't be seven in
just one
column, it's impossible to have any scenario where S is seven and all
attempts can be safely skipped.  That allows the code to zoom in on a
answer faster.

Now that we understand the logic, let's start tackling the code:

  require 'set'

  # State represents the stage of a partially solved word equation.  It
  # keeps track of what digits letters map to, which digits have not yet
  # been assigned to letters, and the results of the last summed column,
  # including the resulting digit and any carry if there is one.
  class State
    attr_accessor :sum, :carry
    attr_reader :letters

    def initialize()
      @available_digits =
      @letters =
      @sum, @carry = 0, 0

    # Return digit for letter.
    def [](letter)

    # The the digit for a letter.
    def []=(letter, digit)
      # if the letter is currently assigned, return its digit to the
      # available set
      @available_digits.add @letters[letter] if @letters[letter]

      @letters[letter] = digit
      @available_digits.delete digit

    # Clear the digit for a letter.
    def clear(letter)
      @available_digits.add @letters[letter]
      @letters[letter] = nil

    # Return the available digits as an array copied from the set.
    def available_digits

    # Tests whether a given digit is still available.
    def available?(digit)
      @available_digits.member? digit

    # Receives the total for a column and keeps track of it as the
    # summed-to digit and any carry.
    def column_total=(total)
      @sum = total % 10
      @carry = total / 10

  # ...

This State object tracks progress through the equation, which will be
column by column.  It has operations to track what each letter is
assigned to, assign letters as they are determined, examine which digits
and have not been used, and track the sum of the last column plus any
carried over to the next column.  There's nothing too tricky in this
structure code.

What we need to go with this, is an algorithm that drives this State
object to a
solution.  That code begins here:

  # ...

  # Step is an "abstract" base level class from which all the "concrete"
  # steps can be derived.  It simply handles the storage of the next
  # step in the sequence.  Subclasses should provide 1) a to_s method to
  # describe the step being performed and 2) a perform method to
  # actually perform the step.
  class Step
    attr_writer :next_step

  # ...

This base Step is about as simple as things get.  I merely provides a
means of
storing the next step in the process.

Note that this class's abstract status and the required implementation
subclasses are all handled through the documentation.  That's perfectly
reasonable in a dynamic language like Ruby where we can count on duck
typing to
resolve to the proper methods when the search is actually being

Let's advance to a concrete implementation of the Step class:

  # ...

  # This step tries assigning each available digit to a given letter and
  # continuing from there.
  class ChooseStep < Step
    def initialize(letter)
      @letter = letter

    def to_s
      "Choose a digit for \"#{@letter}\"."

    def perform(state)
      state.available_digits.each do |v|
        state[@letter] = v

  # ...

This ChooseStep handles the digit guessing.  It is created for some
letter and
when perform() is triggered, it will try each unused in turn digit in
position.  After a new guess is set, the ChooseStep just hands off to a
step to verify that the current guess works.

Here's another Step subclass:

  # ...

  # This step sums up the given letters and changes to state to reflect
  # the sum.  Because we may have to backtrack, it stores the previous
  # saved sum and carry for later restoration.
  class SumColumnStep < Step
    def initialize(letters)
      @letters = letters

    def to_s
      list = { |l| "\"#{l}\"" }.join(', ')
      "Sum the column using letters #{list} (and include carry)."

    def perform(state)
      # save sum and carry
      saved_sum, saved_carry = state.sum, state.carry

      state.column_total =
        state.carry +
        @letters.inject(0) { |sum, letter| sum + state[letter] }

      # restore sum and carry
      state.sum, state.carry = saved_sum, saved_carry

  # ...

This SumColumnStep will be added whenever guesses had been made for an
column.  It's job is to add up that column and update the State with
this new
total.  You can see that it must save old State values and restore them

Once we know a column total, we can use that to set a letter from the
side of the equation:

  # ...

  # This step determines the digit for a letter given the last column
  # summed.  If the digit is not available, then we cannot continue.
  class AssignOnSumStep < Step
    def initialize(letter)
      @letter = letter

    def to_s
      "Set the digit for \"#{@letter}\" based on last column summed."

    def perform(state)
      if state.available? state.sum
        state[@letter] = state.sum

  # ...

This AssignOnSumStep is added for letters in the solution of the
equation.  It
will set the value of that letter to the calculated sum of the column,
that is a legal non-duplicate digit choice.

When we have assigned that letter, we need to verify that the whole
column makes
sense mathematically:

  # ...

  # This step will occur after a column is summed, and the result must
  # match a letter that's already been assigned.
  class CheckOnSumStep < Step
    def initialize(letter)
      @letter = letter

    def to_s
      "Verify that last column summed matches current " +
        "digit for \"#{@letter}\"."

    def perform(state)
      @next_step.perform(state) if state[@letter] == state.sum

  # ...

Now, if we did all the guessing, summing, and assigning everything
probably adds
up.  But as we continue through the equation, some numbers will already
filled in.  Sums created using those may not balance with the total
digit.  This
CheckOnSumStep watches for such a case.

If the sum doesn't check out, this class causes backtracking.  Note that
all it
has to do is not forward to the following steps which will cause
recursion to
unwind the stack until it has another option.

One last check can trim the search space further:

  # ...

  # This step will occur after a letter is assigned to a digit if the
  # letter is not allowed to be a zero, because one or more terms begins
  # with that letter.
  class CheckNotZeroStep < Step
    def initialize(letter)
      @letter = letter

    def to_s
      "Verify that \"#{@letter}\" has not been assigned to zero."

    def perform(state)
      @next_step.perform(state) unless state[@letter] == 0

  # ...

This CheckNotZeroStep is used to ensure that a leading letter in a term
non-zero.  Again, it fails to forward calls when this is not the case.

One more step is needed to catch correct solutions:

  # ...

  # This step represents finishing the equation.  The carry must be zero
  # for the perform to have found an actual result, so check that and
  # display a digit -> letter conversion table and dispaly the equation
  # with the digits substituted in for the letters.
  class FinishStep < Step
    def initialize(equation)
      @equation = equation

    def to_s
      "Display a solution (provided carry is zero)!"

    def perform(state)
      # we're supposedly done, so there can't be anything left in carry
      return unless state.carry == 0

      # display a letter to digit table on a single line
      table = state.letters.invert
      puts { |k| "#{table[k]}=#{k}" }.join('    ')

      # display the equation with digits substituted for the letters
      equation = @equation.dup
      state.letters.each { |k, v| equation.gsub!(k, v.to_s) }
      puts equation

  # ...

This method first ensures that we are successful by validating that we
have no
remaining carry value.  If that's true, our equation balanced out.

The rest of the work here is just in printing the found result.  Nothing

We're now ready to get into the application code:

  # ...

  # Do a basic test for the command-line arguments validity.
  unless ARGV[0] =~'^[a-z]+(\+[a-z]+)*=[a-z]+$')
    STDERR.puts "invalid argument"
    exit 1

  # Split the command-line argument into terms and figure out how many
  # columns we're dealing with.
  terms = ARGV[0].split(/\+|=/)
  column_count = { |e| e.size }.max

  # Build the display of the equation a line at a time.  The line
  # containing the final term of the sum has to have room for the plus
  # sign.
  display_columns = [column_count, terms[-2].size + 1].max
  display  = []
  terms[0..-3].each do |term|
    display << term.rjust(display_columns)
  display << "+" + terms[-2].rjust(display_columns - 1)
  display << "-" * display_columns
  display << terms[-1].rjust(display_columns)
  display = display.join("\n")
  puts display

  # AssignOnSumStep which letters cannot be zero since they're the first
  # letter of a term.
  nonzero_letters =
  terms.each { |e| nonzero_letters.add(e[0, 1]) }

  # A place to keep track of which letters have so-far been assigned.
  chosen_letters =

  # ...

This code validates the input and breaks it into terms.  After that, the
chunk of code here displays the equation in a pretty format, like the
from the quiz description.

The rest of the code begins to divide up the input as needed to build
the proper
steps.  The first tactic is to locate and letters that must be nonzero,
they start a term.  A set is also prepared to hold letters that have be
values at any point in the process.

Here's the heart of the process code:

  # ...

  # Build up the steps needed to solve the equation.
  steps = []
  column_count.times do |column|
    index = -column - 1
    letters = []                 # letters for this column to be added

    terms[0..-2].each do |term|  # for each term that's being added...
      letter = term[index, 1]
      next if letter.nil?        # skip term if no letter in column
      letters << letter          # note that this letter is part of sum

      # if the letter does not have a digit, create a ChooseStep
      unless chosen_letters.member? letter
        steps <<
        steps << if
          nonzero_letters.member? letter

    # create a SumColumnStep for the column
    steps <<

    summed_letter = terms[-1][index, 1]  # the letter being summed to

    # check whether the summed to letter should already have a digit
    if chosen_letters.member? summed_letter
      # should already have a digit, check that summed digit matches it
      steps <<
      # doesn't already have digit, so create a AssignOnSumStep for
      # letter
      steps <<

      # check whether this letter cannot be zero and if so add a
      # CheckNotZeroStep
      steps << if
        nonzero_letters.member? summed_letter

  # ...

This code breaks down the provided equation into the steps we've seen
defined up
to this point.  Though it's a fair bit of code, it's pretty
straightforward and
very well commented.  In short:

  1.  Values are selected for the numbers in each column as needed.
  2.  Columns are summed
  3.  Sums are assigned and or validated as needed.

With the setup complete, here's the code that kicks the solver into

  # ...

  # should be done, so add a FinishStep
  steps <<

  # print out all the steps
  # steps.each_with_index { |step, i| puts "#{i + 1}. #{step}" }

  # let each step know about the one that follows it.
  steps.each_with_index { |step, i| step.next_step = steps[i + 1] }

  # start performing with the first step.

Here the FinishStep is added, all steps are linked, and the perform()
call is
made to get the ball rolling.  You can uncomment the second chunk of
code to
have a human-readable explanation of the steps added to the output.

My thanks to all the super clever solvers who tackled this problem.  I
was blown
away with the creativity.

Tomorrow we will put Ruby Quiz to work helping some friends of ours...
This topic is locked and can not be replied to.