Skip to content

Instantly share code, notes, and snippets.

@djsell
Created Mar 25, 2014
Embed
What would you like to do?
DONT + TAKE = MONEY; M=Y, A=D
def generate_map(letters, numbers, map, words)
return test_sum(map, words) if letters.empty?
letter = letters[0]
numbers.each do |number|
generate_map(
letters - [letter],
numbers - [number],
map.merge(letter => number),
words
)
end
end
def test_sum(map, words)
numbers = []
words.each do |word|
number = 0
word.each_char do |c|
number = number * 10 + map[c]
end
numbers << number
end
puts "#{numbers[0]} + #{numbers[1]} = #{numbers[2]}" if (numbers[0] + numbers[1]) == numbers[2]
end
words = ["DONT", "TDKE", "MONEM"]
letters = words.join("").split("").uniq
letters.delete("M")
generate_map(letters, (2..9).to_a, { "M" => 1 }, words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment