Files
aoc/2015/17.py
PedroEdiaz 62bdc3c593 Opt: 01, 17
2025-10-31 15:57:08 -06:00

48 lines
815 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
"""
Let:
f: i -> X[j] for j in range(len(X)) if i & 1<<j ]
Then:
f is bijection between range(2**len(X)) and the P(X).
Futhermore:
popcount(i) == len(f(i))
"""
def powerset(X):
return [[ X[j] for j in range(len(X)) if i & 1<<j ] for i in range(1<<len(X))]
input= [ int(x) for x in input.split()]
res1, res2, min_l = 0, 0, len(input)
for s in powerset(input):
if sum(s) != 150:
continue
res1 += 1
l = len(s)
if l < min_l:
min_l = l
res2 = 0;
if l == min_l:
res2 += 1;
print(res1, res2)
# One Liner
#print( sum([ sum( [input[j] for j in range(len(input)) if i & 1<<j ]) == 150 for i in range(1<<len(input))] ))