19
2
Bowl Pile Height
The goal of this puzzle is to compute the height of a stack of bowls.
A bowl is defined to be a radially symmetric device without thickness.
Its silhouette shape is an even polynomial. The stack is described by a list of radii, each associated with an even polynomial, given as input as a list of coefficients (e.g. the list 3.1 4.2
represents the polynomial \$3.1x^2+4.2x^4\$).
The polynomial may have arbitrary degree. For simplicity, the height of the pile is defined as the altitude of the center of the top-most bowl (see plot of Example 3 for an illustration).
Test cases are in the format radius:coeff1 coeff2 ...
: each line starts with a float number representing the radius of the bowl, followed by a colon and a space-separated list containing the coefficients for the even powers, starting with power 2 (zero constant part is implied). For example, the line 2.3:3.1 4.2
describes a bowl of radius 2.3
and the shape-polynomial 3.1 * x^2 + 4.2 * x^4
.
Example 1
42:3.141
describes a pile of zero height since a single bowl has no height.
Example 2
1:1 2
1.2:5
1:3
describes a pile of height 2.0
(see plot).
Example 3
1:1.0
0.6:0.2
0.6:0.4
1.4:0.2
0.4:0 10
describes a pile of height 0.8 (see green arrow in the plot).
This is code golf, so the shortest code wins.
I have reference code.
Edit:
The reference implementation relies on a library to compute the roots of polynomials. You may do that as well but you don't need to. Since the reference implementation is only a (quite good) numerical approximation, I will accept any code which produces correct results within common floating-point tolerances.
The idea counts. I don't care if there are small erros \$<\varepsilon\$.
Another variant of this puzzle is to minimize the height by reordering the bowls. I'm not sure if there's a fast solution (I guess it's NP-hard). If anyone has a better idea (or can prove NP-completeness), please tell me!
Comments are not for extended discussion; this conversation has been moved to chat.
– Mego – 2019-08-29T16:03:22.617In your reference code, I believe the body of
is_maximum
should be e.g.return evaluate(differentiate(shape_0), root) > 0.0
. Currently, it evaluates the root usingdd
(derivative of the difference between shapes), which should always return 0 (for roots). Due to floating point errors, the result is occasionally a positive value close to 0, which is why the code outputs a correct or more accurate result some of the time. Check the input1:0.2, 1:0.1 0.2
which should output0.0125
– redundancy – 2019-08-29T20:51:42.280@redundancy it’s actually redundant anyway. The max y value is chosen and 0 will always be in the compares values. – Nick Kennedy – 2019-08-29T21:18:18.597
2In example 3, the final height should be
0.801
. The final two bowls touch at radius0.1
. – attinat – 2019-08-29T23:19:43.700Yes, I got the same result. – Joel – 2019-08-29T23:39:22.683
@Joel agree, now I’ve fixed a bug in my Jelly code – Nick Kennedy – 2019-08-30T00:05:23.323