Skip to content

Instantly share code, notes, and snippets.

@alucky0707
Created May 5, 2013 11:00
Show Gist options
  • Save alucky0707/5520496 to your computer and use it in GitHub Desktop.
Save alucky0707/5520496 to your computer and use it in GitHub Desktop.
AOJ 110
while s = gets
break if s.strip == ""
_ = s.match /([0-9X]+)\+([0-9X]+)\=([0-9X]+)/
a = _[1]
b = _[2]
c = _[3]
answer = nil
(0..9).each do|i|
next if i == 0 && (a != 'X' && a[0] == 'X' ||
b != 'X' && b[0] == 'X' ||
c != 'X' && c[0] == 'X')
x = a.gsub(/X/, i.to_s).to_i
y = b.gsub(/X/, i.to_s).to_i
z = c.gsub(/X/, i.to_s).to_i
if x + y == z
answer = i
break
end
end
puts answer ? answer.to_s : 'NA'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment