AdventOfCode/Python/2024/07/main.py

23 lines
566 B
Python
Raw Normal View History

2024-12-10 09:41:45 -08:00
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)]))