47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
import sys
|
|
from itertools import combinations
|
|
|
|
with open("i24.txt", "r") as f:
|
|
weights = list(map(int, f.readlines()))
|
|
|
|
total_sum = sum(weights)
|
|
|
|
indices = list(range(len(weights)))
|
|
|
|
lowest = None
|
|
def find(part_1=True):
|
|
if part_1:
|
|
target_weight = total_sum // 3
|
|
else:
|
|
target_weight = total_sum // 4
|
|
for group_1_size in range(1, len(weights) - 1):
|
|
for group_1_indices in combinations(indices, group_1_size):
|
|
group_1_weight = sum(weights[i] for i in group_1_indices)
|
|
if not group_1_weight == target_weight:
|
|
continue
|
|
remaining_indices = list(set(indices) - set(group_1_indices))
|
|
for group_2_size in range(1, len(remaining_indices) - 1):
|
|
for group_2_indices in combinations(remaining_indices, group_2_size):
|
|
group_2_weight = sum(weights[i] for i in group_2_indices)
|
|
if not group_2_weight == target_weight:
|
|
continue
|
|
if part_1 :
|
|
r = 1
|
|
for i in group_1_indices:
|
|
r *= weights[i]
|
|
return r
|
|
else:
|
|
remaining_indices = list(set(indices) - set(group_1_indices))
|
|
for group_3_size in range(1, len(remaining_indices)- 1):
|
|
for group_3_indices in combinations(remaining_indices, group_3_size):
|
|
group_3_weight = sum(weights[i] for i in group_3_indices)
|
|
if not group_3_weight == target_weight:
|
|
continue
|
|
r = 1
|
|
for i in group_1_indices:
|
|
r *= weights[i]
|
|
return r
|
|
|
|
print(find(True))
|
|
print(find(False))
|