advent-of-code/2024/7/one.py
2024-12-25 12:59:49 +01:00

35 lines
758 B
Python

#!/usr/bin/env python3
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
final = 0
for line in lines:
spli = line.split()
res = int(spli[0][:-1])
nums = [int(num) for num in spli[1:]]
def check(tot: int, nums: list[int]) -> bool:
for op in (int.__add__, int.__mul__):
ntot = op(tot, nums[0])
if ntot > res:
continue
if len(nums) == 1:
if ntot == res:
return True
else:
if check(ntot, nums[1:]):
return True
return False
if check(nums[0], nums[1:]):
final += res
print(final)
# 2664444091381: too low