17: Add proof
This commit is contained in:
51
2015/17.py
51
2015/17.py
@@ -1,43 +1,42 @@
|
||||
# Fill Input
|
||||
|
||||
input = ""
|
||||
|
||||
try:
|
||||
with open("17.txt", 'r', encoding='utf-8') as file:
|
||||
input += file.read()
|
||||
input = file.read()
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
# Check if sum of permutation equal 150
|
||||
def sum_equal_150(input, i):
|
||||
sum = 0
|
||||
for j in range(l):
|
||||
if i & 1<<j:
|
||||
sum += input[j]
|
||||
if sum > 150:
|
||||
return False
|
||||
return sum == 150
|
||||
|
||||
# Reverse sort input
|
||||
input= [ int(x) for x in input.split()]
|
||||
input.sort(reverse=True)
|
||||
|
||||
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 ]
|
||||
|
||||
for i in range(2**l):
|
||||
if not sum_equal_150(input, i):
|
||||
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
|
||||
|
||||
popcount = i.bit_count()
|
||||
|
||||
if popcount < min_popcount:
|
||||
min_popcount = popcount
|
||||
res2 = 0;
|
||||
|
||||
if popcount == min_popcount:
|
||||
res2 += 1;
|
||||
|
||||
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)] ))
|
||||
|
||||
Reference in New Issue
Block a user