-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-01.py
36 lines (36 loc) · 974 Bytes
/
07-01.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
inputval = ""
total = 0
def eval_expr(ins, ops):
stack = [ins[0]]
if ins == [11, 6, 16, 20] and ops == ["+", "*", "+"]:
pass
for i, val in enumerate(ins[1:]):
if ops[i] == "*":
stack.append(stack.pop() * val)
else:
stack.append(stack.pop() + val)
return sum(stack)
def gen_combination(n):
result = ["+"] * n
while True:
yield result
idx = n - 1
while idx >= 0 and result[idx] == "*":
result[idx] = "+"
idx -= 1
if idx < 0:
break
result[idx] = "*"
while True:
inputval = input()
if not inputval:
break
result, rest = inputval.split(":")
values = list(map(int, rest.strip().split(" ")))
result = int(result)
for ops in gen_combination(len(values) - 1):
if eval_expr(values, ops) == result:
total += result
print(result, values, ops)
break
print(total)