23 lines
566 B
Python
23 lines
566 B
Python
|
|
input = open("input", 'r')
|
||
|
|
|
||
|
|
d = []
|
||
|
|
for line in input:
|
||
|
|
ans, nums = line.split(": ")
|
||
|
|
d.append((int(ans), [int(num) for num in nums.split(" ")]))
|
||
|
|
|
||
|
|
def run(ans, eq, p2=False):
|
||
|
|
if len(eq) == 1:
|
||
|
|
return ans == eq[0]
|
||
|
|
ops = {lambda x,y: (x*y), lambda x,y: (x+y)}
|
||
|
|
if p2:
|
||
|
|
ops.add(lambda x,y: int(str(x) + str(y)))
|
||
|
|
|
||
|
|
x,y,*eqs = eq
|
||
|
|
for op in ops:
|
||
|
|
if run(ans, [op(x,y)] + eqs, p2):
|
||
|
|
return True
|
||
|
|
return False
|
||
|
|
|
||
|
|
print(sum([ans for ans, eq in d if run(ans,eq)]))
|
||
|
|
print(sum([ans for ans, eq in d if run(ans,eq,True)]))
|