2020-12-18 06:13:07 +00:00
|
|
|
import fileinput
|
|
|
|
|
|
|
|
|
2020-12-22 15:15:18 +00:00
|
|
|
def find_matching_parenthesis(line):
|
|
|
|
depth = 1
|
|
|
|
for i, c in enumerate(line[1:]):
|
|
|
|
if c == "(":
|
|
|
|
depth += 1
|
|
|
|
if c == ")":
|
|
|
|
depth -= 1
|
|
|
|
if depth == 0:
|
|
|
|
break
|
|
|
|
return i
|
|
|
|
|
|
|
|
|
2020-12-18 06:13:07 +00:00
|
|
|
def solve(line):
|
2020-12-22 15:15:18 +00:00
|
|
|
print("".join(line))
|
2020-12-18 06:13:07 +00:00
|
|
|
if line[0].isdigit():
|
|
|
|
if len(line) == 1:
|
|
|
|
return int(line[0])
|
2020-12-22 15:15:18 +00:00
|
|
|
|
|
|
|
if len(line) == 3:
|
|
|
|
if line[1] == "+":
|
|
|
|
return int(line[0]) + int(line[2])
|
|
|
|
if line[1] == "*":
|
|
|
|
return int(line[0]) * int(line[2])
|
|
|
|
|
|
|
|
if line[1] == "+":
|
|
|
|
if line[2] == "(":
|
|
|
|
closing = find_matching_parenthesis(line[2:])
|
|
|
|
inner = int(line[0]) + solve(line[3 : closing + 4])
|
|
|
|
return solve([str(inner)] + line[closing + 5 :])
|
2020-12-18 06:13:07 +00:00
|
|
|
else:
|
|
|
|
v = solve(line[:3])
|
|
|
|
return solve([str(v)] + line[3:])
|
2020-12-22 15:15:18 +00:00
|
|
|
|
|
|
|
if line[1] == "*":
|
2020-12-18 06:13:07 +00:00
|
|
|
return solve(line[:1]) * solve(line[2:])
|
|
|
|
raise BaseException(f"HELP {repr(line)}")
|
|
|
|
|
2020-12-22 15:15:18 +00:00
|
|
|
if line[0] == "(":
|
|
|
|
closing = find_matching_parenthesis(line[1:])
|
|
|
|
inner = str(solve(line[1 : closing +2]))
|
|
|
|
return solve([inner] + line[closing +3 :])
|
2020-12-18 06:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
summ = 0
|
|
|
|
for line in fileinput.input():
|
|
|
|
line = line.replace(" ", "").strip()
|
|
|
|
v = solve(list(line))
|
|
|
|
print(line, v)
|
|
|
|
summ += v
|
|
|
|
print(summ)
|