Skip to content

Instantly share code, notes, and snippets.

@ymurase
Created June 19, 2014 23:22
Show Gist options
  • Save ymurase/f4551f36d6c41aaaf134 to your computer and use it in GitHub Desktop.
Save ymurase/f4551f36d6c41aaaf134 to your computer and use it in GitHub Desktop.
def alphametic(input)
candidate = (input =~ /[\+=]X/ or input =~ /^X/) ? 1..9 : 0..9
candidate.find {|i| eval(input.gsub('X', i.to_s).sub('=', '==')) } || "NA"
end
if __FILE__ == $0
while line = gets
puts alphametic(line.chomp)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment