mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 05:10:32 +01:00
Improve solution for day 17.
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
from __future__ import print_function, division
|
||||
import fileinput
|
||||
from collections import defaultdict
|
||||
import bisect
|
||||
|
||||
buckets = []
|
||||
def value(buckets, choice):
|
||||
total = 0
|
||||
for value in buckets:
|
||||
if choice % 2 == 1:
|
||||
total += value
|
||||
|
||||
for line in fileinput.input():
|
||||
buckets.append(int(line))
|
||||
choice //= 2
|
||||
|
||||
def works(bucketCombination, target):
|
||||
for idx, value in enumerate(buckets):
|
||||
|
||||
if bucketCombination % 2 == 1:
|
||||
target -= value
|
||||
|
||||
bucketCombination = bucketCombination // 2
|
||||
|
||||
return target == 0
|
||||
return total
|
||||
|
||||
def ones(x):
|
||||
n = 0
|
||||
@@ -27,11 +23,40 @@ def ones(x):
|
||||
|
||||
return n
|
||||
|
||||
def partition(a_list):
|
||||
pivot = len(a_list) // 2
|
||||
|
||||
return a_list[:pivot], a_list[pivot:]
|
||||
|
||||
def partitionList(buckets):
|
||||
result = [(value(buckets, x), ones(x)) for x in range(1 << len(buckets))]
|
||||
result.sort()
|
||||
return result
|
||||
|
||||
buckets = []
|
||||
|
||||
for line in fileinput.input():
|
||||
buckets.append(int(line))
|
||||
|
||||
partition1, partition2 = partition(buckets)
|
||||
|
||||
values1 = partitionList(partition1)
|
||||
values2 = partitionList(partition2)
|
||||
|
||||
possible = defaultdict(lambda: 0)
|
||||
|
||||
for x in range(1 << len(buckets)):
|
||||
if works(x, 150):
|
||||
n = ones(x)
|
||||
possible[n] += 1
|
||||
i = 0
|
||||
|
||||
target = 150
|
||||
|
||||
for entry in values1:
|
||||
|
||||
i = bisect.bisect_left(values2, (target - entry[0], 0))
|
||||
|
||||
while i < len(values2) and entry[0] + values2[i][0] == target:
|
||||
possible[entry[1] + values2[i][1]] += 1
|
||||
i += 1
|
||||
|
||||
print("Total possibilities:", sum(possible.values()))
|
||||
print("Minimal possibilities:", possible[min(possible.keys())])
|
||||
|
||||
print(sum(possible[x] for x in possible), possible[min(possible.keys())])
|
||||
|
||||
Reference in New Issue
Block a user