Sha256: e6dd7d726c7d88e0b3091876a52ca22288050b9d6460ac9a92d1a7ca191a3234

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

require 'rubygems'
require 'gecoder'

solution = Gecode.solve do
  # A helper to make the linear equation a bit tidier. Takes a number of
  # variables and computes the linear combination as if the variable
  # were digits in a base 10 number. E.g. x,y,z becomes
  # 100*x + 10*y + z .
  def equation_row(*variables)
    variables.inject{ |result, variable| variable + result*10 }
  end

  # Set up the variables. 
  # Let "letters" be an array of 8 integer variables with domain 0..9.
  # The elements represents the letters s, e, n, d, m, o, r and y.
  letters_is_an int_var_array(8, 0..9)
  s,e,n,d,m,o,r,y = letters

  # Set up the constraints.
  # The equation must hold.
  (equation_row(s, e, n, d) + equation_row(m, o, r, e)).must ==
    equation_row(m, o, n, e, y)

  # The initial letters may not be 0.
  s.must_not == 0
  m.must_not == 0

  # All letters must be assigned different digits.
  letters.must_be.distinct

  # Tell Gecode what variables we want to know the values of.
  branch_on letters, :variable => :smallest_size, :value => :min
end

puts 's e n d m o r y'
puts solution.letters.values.join(' ')

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
gecoder-0.9.0 example/money.rb
gecoder-with-gecode-0.9.0-x86-mswin32-60 example/money.rb
gecoder-with-gecode-0.9.0 example/money.rb