-
Notifications
You must be signed in to change notification settings - Fork 0
/
d7-sol.py
72 lines (58 loc) · 1.87 KB
/
d7-sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import math
import copy
def checkValidityA(target: int, inputs: list):
# Bound
tot = sum(inputs)
prod = math.prod(inputs)
if target < min(tot, prod) - len(inputs) or target > max(tot, prod) + max(inputs) * inputs.count(1):
# if target == 136144 and target > max(tot, prod):
# print(target, inputs, max(tot, prod))
return False
elif target == prod or target == tot:
return True
if len(inputs) == 1:
return tot == target
# Branch
add = copy.deepcopy(inputs)
mult = copy.deepcopy(inputs)
back = add.pop()
add[-1] += back
back = mult.pop()
mult[-1] *= back
return checkValidityA(target, mult) or checkValidityA(target, add)
def checkValidityB(target: int, inputs: list):
if len(inputs) == 1:
return inputs[0] == target
add = copy.deepcopy(inputs)
mult = copy.deepcopy(inputs)
back = add.pop()
add[-1] += back
back = mult.pop()
mult[-1] *= back
# print(inputs)
back = inputs.pop()
# print(inputs[-1], back, int(str(inputs[-1]) + str(back)), str(inputs[-1]) + str(back))
inputs[-1] = int(str(back) + str(inputs[-1]))
return checkValidityB(target, inputs) or checkValidityB(target, add) or checkValidityB(target, mult)
totalA = 0
totalB = 0
# count = 0
for l in sys.stdin:
eqn = l.partition(':')
target = int(eqn[0])
inputs = eqn[2].split()
inputs = [int(i) for i in inputs]
#print(target, inputs, end=' ')
inputs.reverse()
if checkValidityA(target, inputs):
# print(target)
totalA += target
# count += 1
# print(inputs)
if checkValidityB(target, inputs):
totalB += target
# print(count)
print("The total calibration result is:", totalA)
print("The total calibration result, including concatenation, is:", totalB)
# Horribly inefficient, but does the job