Drawer challenge¶

Here is a straighforward solution, not actually optimal, but good enough to do the job.

In [2]:
import itertools
drawers = [6, 16, 8, 10, 9]
total_height = 30
leeway = 16
for nb in range(1000):
    # This optimization is not mandatory
    if nb*min(drawers) > total_height:
        break
    for combination in itertools.combinations_with_replacement(drawers, nb):
        if total_height - leeway <= sum(combination) <= total_height:
            print(",".join(map(str, sorted(combination))))
16
6,16
6,8
6,10
6,9
8,16
10,16
9,16
8,8
8,10
8,9
10,10
9,10
9,9
6,6,6
6,6,16
6,6,8
6,6,10
6,6,9
6,8,16
6,8,8
6,8,10
6,8,9
6,10,10
6,9,10
6,9,9
8,8,8
8,8,10
8,8,9
8,10,10
8,9,10
8,9,9
10,10,10
9,10,10
9,9,10
9,9,9
6,6,6,6
6,6,6,8
6,6,6,10
6,6,6,9
6,6,8,8
6,6,8,10
6,6,8,9
6,6,9,9
6,8,8,8
6,6,6,6,6
In [ ]: