The sum of some parts
05AB1E, 10 bytes
æʒø`O²‹_i,
Try it online!
Explanation
æ # compute powerset of input
ʒ # filter out falsy values (we don't actually filter, it just saves a byte)
ø # zip into a list of keys and a list of values
` # push separately to the stack
O # sum values
²‹_i # if not less than input_2
, # print list of keys
Mathematica, 45 43 bytes
Saved 2 bytes due to alephalpha.
Keys/@Cases[Subsets@#,a_/;Tr@Values@a>=#2]&
Anonymous function. Takes a list of rules (e.g., {"Part 1"->20,"Part 2"->15}
) and a number as input and returns a list of lists of strings as output.
Japt, 15 14 12 bytes
Saved 2 bytes thanks to @obarakon
à f_xŨVÃm®g
Test it online! Output is a nested array, but it's rather hard to tell so the -R
flag has been added to put each item on its own line.
Explanation
à f_ xÅ ¨ VÃ m® g
Uà fZ{Zxs1 >=V} mmZ{Zg}
// Implicit: U = input array, V = input number
Uà // Take all combinations of U.
fZ{ } // Filter to only the combinations Z where this is true:
Zxs1 // The sum of each [item of Z]'s item at index 1
>=V // is greater than or equal to V.
m // Map each combination in the result by
mZ{ } // mapping each array Z in that to
Zg // the first item in Z.
// This replaces each key-value pair with its key.
// Implicit: output result of last expression
An interesting thing to note is the use of Å
(or s1
) instead of g1
. s1
on an array simply returns everything but the first item. Since each array only has two items (a key and a value), this returns an array containing the value. x
then uses JS's native coercion to convert this into a number, and on an array containing one number, this happens to return that number.