[2020][ruby][18.x]

pull/2/head
Alpha Chen 4 years ago
parent 5949372ee0
commit 019d9eae12

@ -0,0 +1,32 @@
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
Loading…
Cancel
Save