4 ms·
This seems to be false: eggs :: [Int] -> Int -> Int eggs drops floor = if floor >= last drops then (length drops) - 1 else let
by philh 14y ago
This seems to be false:
eggs :: [Int] -> Int -> Int
eggs drops floor =
if floor >= last drops
then (length drops) - 1
else let numless = length $ filter (<= floor) drops
below = last $ filter (<= floor) drops
above = head $ filter (> floor) drops
in numless + (floor - below) + (if floor + 1 == above then 0 else 1)
sum $ map (eggs [0,10 .. 100]) [0..100] -- returns 1100
sum $ map (eggs [0,14,27,39,50,60,69,77,84,90,95,99,100]) [0..100] -- returns 1047
(Not 100% confident that I haven't made a mistake. You pass "eggs" a list of floors to drop the first egg on, and the lowest floor that the egg will survive being dropped from. It returns the number of drops it takes to discover that value. The list needs to start with 0 because that requirement simplifies the function slightly.
But if I haven't made a mistake, "10, 20, 30..." takes an average of 10.9 drops and "14, 27, 39..." takes an average of 10.4. That's assuming the floor is uniformly distributed between 0 and 100 inclusive.)