1.py (717B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 4 def parse(): 5 cases = [] 6 for l in sys.stdin: 7 target = int(l.split(":")[0]) 8 nums = [int(x) for x in l.split(" ")[1:]] 9 cases.append((target, nums)) 10 return cases 11 12 def check(case): 13 target, nums = case 14 15 # Since we don't need to know what operators we use, 16 # we can just recursively check the patterns) 17 def rchk(run, rem): 18 if not rem: 19 return run == target 20 return rchk(run * rem[0], rem[1:]) or rchk(run + rem[0], rem[1:]) 21 22 if rchk(nums[0], nums[1:]): 23 return target 24 return 0 25 26 if __name__ == '__main__': 27 cases = parse() 28 tot = 0 29 for c in cases: 30 tot += check(c) 31 print(tot)