-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres7-2.py
32 lines (30 loc) · 993 Bytes
/
res7-2.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
sum = 0
def convert_to_base_3(num):
result = []
for i in range(3 ** num):
val = i
representation = ""
for _ in range(num):
a = val % 3
representation += str(a)
val = int((val - a) / 3)
result.append(representation[::-1])
return result
with open('input7.txt', 'r') as f:
for n in f:
res = int(n.split(":")[0])
list_of_numbers = [int(x) for x in n.split(":")[1].split()]
combinations = convert_to_base_3(len(list_of_numbers) - 1)
for comb in combinations:
val = list_of_numbers[0]
for j, num in enumerate(list_of_numbers[1:]):
if comb[j] == "0":
val += num
elif comb[j] == "1":
val *= num
else:
val = int(str(val) + str(num))
if val == res:
sum += res
break
print(sum)