#!/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