Part of Advent of Code Golf 2021 event. See the linked meta post for details.
The story continues from AoC2015 Day 24, Part 2.
Here's why I'm posting instead of Bubbler
To recap: Santa gives you the list of gift packages' weights. The packages must be split into multiple groups so that each group has the same weight, so that the sleigh is balanced and Santa can defy physics to deliver the presents.
Also, one of the groups goes to the passenger compartment, and Santa wants to have the maximal legroom, so the number of packages in there must be minimal. If there are ties, choose the one with the minimal "quantum entanglement" - the product of all weights in that group - to minimize the risk of physics doing its job, so to speak.
If you were to divide [1, 2, 3, 4, 5, 7, 8, 9, 10, 11]
into three groups of equal weight, the possible choices are (QE stands for Quantum Entanglement):
Group 1; Group 2; Group 3
11 9 (QE= 99); 10 8 2; 7 5 4 3 1
10 9 1 (QE= 90); 11 7 2; 8 5 4 3
10 8 2 (QE=160); 11 9; 7 5 4 3 1
10 7 3 (QE=210); 11 9; 8 5 4 2 1
10 5 4 1 (QE=200); 11 9; 8 7 3 2
10 5 3 2 (QE=300); 11 9; 8 7 4 1
10 4 3 2 1 (QE=240); 11 9; 8 7 5
9 8 3 (QE=216); 11 7 2; 10 5 4 1
9 7 4 (QE=252); 11 8 1; 10 5 3 2
9 5 4 2 (QE=360); 11 8 1; 10 7 3
8 7 5 (QE=280); 11 9; 10 4 3 2 1
8 5 4 3 (QE=480); 11 9; 10 7 2 1
7 5 4 3 1 (QE=420); 11 9; 10 8 2
Since [11, 9]
has the smallest number of packages, you should choose that one.
It turns out that Santa's sleigh is a pretty advanced model and has a bunch of trunks instead of one. Under the same conditions, determine which presents should go into the passenger compartment.
Input: The total number of groups n
, and the list of packages' weights. n
is at least 3, and it is guaranteed that the list of weights can be split into n
groups of equal weight.
Output: The optimal list of packages to be placed in the passenger compartment. The individual packages may be output in any order. If there are multiple optimal solutions (same number of packages and same quantum entanglement), output any one of them.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
Weights, Groups -> Answer
[1, 2, 3, 4, 5, 7, 8, 9, 10, 11], 3 -> [11, 9]
[1, 2, 3, 4, 5, 7, 8, 9, 10, 11], 4 -> [11, 4]
[1, 2, 3, 4, 5, 7, 8, 9, 10, 11], 5 -> [11, 1]
[1, 1, 2, 2, 6, 6, 6, 6, 9], 3 -> [1, 6, 6] or [2, 2, 9]
[1, 2, 2, 3, 3, 3, 4, 6, 6, 9], 3 -> [4, 9]
[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2], 3 -> [3, 2, 2, 2]
[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2], 3 -> [3, 2, 2, 2]
\$\endgroup\$[3, 3, 3]
would be the smallest compartment, but of course that stops you from dividing the other presents evenly between the other two compartments. \$\endgroup\$