Fabergé Easter Eggs crush test
Thought Process
The actual durability of each egg is irrelevant to the problem, the function should be able to return a confident answer without any form of correlation to determine that value.
Known Test Cases
Eggs | Tries | Floors |
---|---|---|
0 | 14 | 0 |
2 | 0 | 0 |
2 | 14 | 105 |
7 | 20 | 137979 |
7 | 500 | 1507386560013475 |
Solving
The answer will always be greater than or equal to the nth triangle number of the number of tries, since the number of remaining tries is the amount of floors that can be tested in the worst case scenario.
JavaScript
// JS bigInts has already been preloaded in constant BigNumber
// Usage: https://github.com/MikeMcl/bignumber.js/
/**
* Calculates the maximum height an egg can be dropped from
* without breaking.
* @param {Number} eggs Number of eggs (<= 20000)
* @param {Number} tries Number of tries (<= 20000)
* @returns {Number} The maximum height
*/
function height(eggs, tries) {
// If no eggs or no tries, there is no floor from which the
// function can succeed
if (eggs == 0 || tries == 0) return new BigNumber(0);
// Drop it from the floor that is equal to the number of tries,
// then decrement and repeat; that way if the egg breaks, you
// can try every floor descending and find the max
let answer = 0;
for (let i = tries; i > 0; i--) {
answer += i;
}
answer = new BigNumber(answer);
return answer;
}