[Community Puzzle] The leaking bathtub

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @Tatur2000,validated by @Razovsky,@Diabetos and @sammck.
If you have any issues, feel free to ping them.

1 Like

Passed all tests, can’t pass validator 2 and 5. I use cm per seconds, so when water’s level get to (h-level) I get time needed to fill bath, then translate seconds to hours and minutes.
Types of variables are double (C++).

Have done by another method: counted time in depends of baths volume devided on filliing volume till leaks point.
Passed all tests, but didn’t pass validator 3

Hello, great problem so far! I had the same first approach as @Oliverri , very inefficient, I got performance issues on the last test. This made me rethink the problem and I found a much more elegant solution. I think its pretty much the same as Oliverri’s second method actually.
I pass all the tests but not the first validator, and I don’t get why. Do you have some hints? Thanks! :slightly_smiling_face:

EDIT: oh, I think I got something. Now I’m computing minutes and deducing seconds and hours from it, for test 2 I get 17.40926 minutes, so 17 minutes and 0.40926 * 60 = 24.5556 seconds. I round to 25 but the expected answer is 24. Why is that? Am I losing precision somewhere?

Description says that result should be rounded down so 24.5556 should be displayed as 24 seconds

Hello Yatech, thanks for noticing. Okay, so rounding down is the same as truncating the floating part, which is what I’ve done before coming to this post: when truncating, I pass all the tests but not the first validator and I don’t quite get why…
It looks like I’m missing a whole second, because if I submit a code that adds 1 second to my existing code, the first validator passes.

Most likely it’s the problem with precision. Try using double instead of float :slight_smile:

For validator 1 the total minutes have decimal part .333333333333336.
If the type you are using is not precise enough this will turn into 19 seconds instead of 20.

Thanks for the insight, that was a precision issue indeed :slight_smile:
However I didn’t have to use double instead of float, because such a piece of code:

float m =  0.333333333333336f;
int s = (int)((m - (int)m) * 60);

Returns the expected result (20).
So instead, I used a comparison like this:

int seconds = (int)((minutes - (int)minutes) * 60);
float secondsF = (minutes - (int)minutes) * 60;
if (Math.Abs(secondsF - (float)(seconds + 1)) < 0.001f)

Seems to do the trick. Let me know if you have a better solution because it still feels wonky…
Anyway, thanks again!

My C# code is published if you want ot take a look.
I just used double and formatted the answer using TimeSpan.