small simplification

This commit is contained in:
Juan José Gutiérrez de Quevedo Pérez 2020-12-18 11:20:38 +01:00
parent 06d29f3857
commit 8969db5c8c

19
18/2.py
View file

@ -2,11 +2,11 @@ with open("input") as f:
content = [x.strip().replace(")", " )").replace("(", "( ").split(" ") for x in f] content = [x.strip().replace(")", " )").replace("(", "( ").split(" ") for x in f]
def evaluate(expression, startpos): def evaluate(expression, startpos):
result = 0 sumresult = 0
number = 0 number = 0
i = startpos i = startpos
expression2 = [] endpos = None
endpos = startpos mulresult = 1
while i < len(expression): while i < len(expression):
number = None number = None
if expression[i] == "(": if expression[i] == "(":
@ -15,20 +15,17 @@ def evaluate(expression, startpos):
endpos = i endpos = i
break break
elif expression[i] == "*": elif expression[i] == "*":
expression2.append(result) mulresult *= sumresult
result = 0 sumresult = 0
elif expression[i] != "+": elif expression[i] != "+":
number = int(expression[i]) number = int(expression[i])
if number is not None: if number is not None:
result += number sumresult += number
i += 1 i += 1
expression2.append(result) mulresult *= sumresult
result = 1
for e in expression2:
result *= e
return result, endpos return mulresult, endpos
print(sum(map(lambda x: evaluate(x, 0)[0], content))) print(sum(map(lambda x: evaluate(x, 0)[0], content)))