Files
aoc/2015/17.py
2025-10-30 00:10:25 -06:00

43 lines
820 B
Python

# Fill Input
try:
with open("17.txt", 'r', encoding='utf-8') as file:
input = file.read()
except Exception as e:
print(f"An error occurred: {e}")
# Reverse sort input
input= [ int(x) for x in input.split()]
l = len(input)
res1, res2, min_popcount = 0, 0, l
for i in range(1<<l):
"""
Let:
f: i -> [input[j] for j in range(len(X)) if i & 1<<j ]
Then:
f is bijection between range(2**len(X)) and the subsets of X.
Futhermore:
popcount(i) == len(f(i))
"""
if sum([input[j] for j in range(l) if i & 1<<j ]) != 150:
continue
res1 += 1
popcount = i.bit_count()
if popcount < min_popcount:
min_popcount = popcount
res2 = 0;
if popcount == min_popcount:
res2 += 1;
print(res1, res2)
# One Liner
print( sum([ sum( [input[j] for j in range(l) if i & 1<<j ]) == 150 for i in range(1<<l)] ))