January 13, 2026

Solution for Advent of Code 2018 - Day 17: Reservoir Research

Link to the puzzle text

Part 1

In this puzzle, we are given a grid of terrain. Each cell is initially either empty or filled with clay. Starting from the top, water will drop down until it reaches clay. It then fills the horizontal line if there are clay borders on both sides or drops down further if the water reaches an edge. We should count the number of total cells reached by the water.

We model the grid as a 2-d array initially filled with a symbol for empty. In our function for dropping water down, we first drop down until we reach a symbol for clay or other water. If we reach clay, we then search for clay borders on both sides. If we find two borders, we can fill the line between the borders with a symbol for standing water and start the next water drop from above. Otherwise, we fill the line with a symbol for non-standing water and then drop water from one or both of the edges by calling the function again with a different starting point. Since the iteration depth was low, we did not need to convert the recursive to a loop-based approach.

Finally we count the number of symbols in the grid and return only the number of standing and non-standing water cells. 

Part 2

In part 2, we are only interested in the number of cells with standing water.

We can use the same code as in part 1 and return only the number of cells with the standing water symbols. 

Link to my solutions

No comments:

Post a Comment