# [Community Puzzle] Retaining Water

https://www.codingame.com/training/easy/retaining-water

Created by @Westicles,validated by @Wontonimo,@selenae and @murrayr.
If you have any issues, feel free to ping them.

Itâs a good puzzle but if you donât pay much attention to the cover and go directly to the IDE, you have to guess a bit cause the Problem doesnât state much.

For example the whole structure is made of cubes, itâs never said at any point.

Also the word âpoolâ is a bit misleading, really a pool looks nothing like that.
It looks more like moutain areas with lakes of retained water than one single pool.

But despite that, it was a nice little puzzle.

3 Likes

For many puzzles, the tags give an additional hint. Here, the lack of a tag is a hint itself: My first (wrong) reaction when reading the statement was âHey, a floodfill in the easy category?â. Then I looked at the tagsâŚ
Nice puzzle, I liked it!

1 Like

I admit that I used unnecessary floodfills cause I followed my firsy idea which was:
for each level h from 1 to 25:
pour 1 volume of water in lvl h tiles if it doesnât leak (and mark all tiles that leak)

But indeed a top-bottom âone huge canadairâ approach is even simpler and doesnât require any floodill.

LOL.
Yes, I wasted a bit of water, but conversing water was not part of the design targetâŚ

To better understand the puzzle:

What is result for 3x3 all X pool

XXX
XXX
XXX

Does water leak diagonally, what is result for:

XZZ
ZXZ
ZZZ

All Xâs would be zero, since you are pouring water on a 3x3 stack of blocks 24 high surrounded by an outside area of zero height. All the water would run off and none would be retained. If the middle value is lower (like in your second example, where the answer would be 2), some water would pool.

I added a note that water does not leak diagonally, just to make it crystal clear.

2 Likes

Ok, Thanks

So result is 2 for:

XZZ
ZXZ
ZZZ

1 Like

The puzzle deserves medium imo, but itâs a good one

8 Likes

very nice puzzle ! I dried my brain to find a correct and naturel way to fill this pool until disasterâŚ congrats.

1 Like

the explanation should probably be a bit clearer, but I had a lot of fun with this puzzle and was a tough one. an extended floodfill problem. Very good idea

3 Likes

Agreed. Once I understand that its like spilling water into the pool and count how much water stayed inside the pool it was a lot easier for me. Like the puzzle, very nice!

Hello !
Iâm really a beginner in coding and this puzzle seems to me much more difficult than the other easy puzzle. There are everything in the explanations to understand the puzzle but itâs really not that obvious at the first reading.
The lack of tags or link to understand how to solve this problem are another negative points. Itâs when I read the forum that I understand that I have to used a floodfill algorithm to solve the problem and even at this stage the algorithm that I code is too slow (I tried to work with a Four-way flood fill using a queue for storage).
I think I will wait to progress to solve this puzzle but I wanted to tell you that for me itâs not an easy puzzle. If you want to let it in easy maybe write a more detailed explanation with some clues to help to built the algorithm or some link to a lesson/video who explained how to solve (just with big step) the problem or at least this floodfill algorithm

1 Like

You donât actually need floodfill here, itâs just one way to do it among several.
The solution can be very short and simple without nothing fancy.
Thatâs why itâs labelled easy, but it doesnât mean the âsimpleâ method is super easy to find, you need to give it some thoughts.

About tags, I think at some point if you really want to progress you shouldnât read them at all and approach any problem with a fresh mind, ready to try anything.

Also about giving a link to a page that explains the method, Iâm not sure itâs a good idea in general. The only puzzles where I found it was legitimate is the collection of puzzle by aCat that aim at teaching classical algorithms(minimax with alpha beta pruning, MCTS, etc), and make sure you understood how to implement them.

1 Like

Wanted a better understanding of what the test cases look like.
Sharing for anyone who finds it useful: https://codepen.io/Abrman/full/abpKbRW

18 Likes

This is at least medium. Or I missed a more obvious easier solution. but enjoyed solving it.

3 Likes

A very interesting puzzle, and indeed I wonder whether itâs an âeasyâ puzzle.

I can see two possible answers:

1. âeasyâ refers to the CS methods that are required to solve the puzzle. In this case, the main method needed is flood-fill (whether explicitly implemented, or implicitly implemented as suggested above by by @pardouin, et al). Is this considered âeasyâ in terms of Codingame standards?
In any case I believe both explicit and implicit implementations fall under âflood fillâ
2. âeasyâ refers to the algorithmical difficulty, in which case this puzzle would possibly earned a âmediumâ, at least compared to other puzzles in the easy category

I wish I could write more about the question of âflood vs. scansâ and is it really different, but then it might hurt the experience of people who havnât solved it yet. If anyone would like to continue this discussion in private - please

3 Likes

Very nice puzzle! I have enjoyed it.

1 Like

This was fun! It does seem very challenging for an easy puzzle. If there is an easy solution, itâs a hard solution to find.

If someone has found a solution to this that doesnât involve flood fill, it would be nice to post a hint about it here.

1 Like

It has been hinted above: imagine a huge canadair dropping a massive volume of water at once. Then, as long as you can find a spot where water can leak outside the map/flow to a lower spot, make it leak/flow.

2 Likes