You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
671 B
33 lines
671 B
require "strscan"
|
|
|
|
def evaluate(input)
|
|
while input.include?(?()
|
|
input = input.gsub(/\((?~[()])+\)/) {|m| evaluate(m[1..-2]) }
|
|
end
|
|
|
|
# ss = StringScanner.new(input)
|
|
# n = ss.scan(/\d+/).to_i
|
|
# until ss.eos?
|
|
# case
|
|
# when ss.scan(/\s*\+\s*(\d+)/)
|
|
# n += ss.captures[0].to_i
|
|
# when ss.scan(/\s*\*\s*(\d+)/)
|
|
# n *= ss.captures[0].to_i
|
|
# else
|
|
# fail
|
|
# end
|
|
# end
|
|
# n
|
|
|
|
while input.include?(?+)
|
|
input = input.gsub(/\d+\s*\+\s*\d+/) {|m| eval(m) }
|
|
end
|
|
while input.include?(?*)
|
|
input = input.gsub(/\d+\s*\*\s*\d+/) {|m| eval(m) }
|
|
end
|
|
input.to_i
|
|
end
|
|
|
|
p ARGF.read.split("\n").map {|line| evaluate(line) }.sum
|
|
|