Modern C++ in Advent of Code: Day 18
On day eighteen of Advent of Code we are working with voxels to calculate the surface of lava droplets.
I do encourage you to try to solve it yourself first https://adventofcode.com.
Input
Our input today is a series of cubes. We will create a custom type to represent a cube and take the input as std::vector<Cube>
.
Counting uncovered sides
For part one, we need to count the total number of sides of our lava voxels that are not covered by other voxels.
Since we are working with a small number of cubes, we can do an exhaustive search, iterating over the neighbours of each cube and checking whether such a cube exists in our input.
If there is no such cube, that side of the cube is not covered and vice-versa.
Ignoring internal pockets
Part two adds a significant complication. We need only count the sides touching the external space and ignore the internal pockets.
Fortunately, we are working with a small number of cubes and a small space (19x19x19 for my input). Therefore, we can use an exhaustive search (exploring the external space) and count the sides of cubes we encounter.
We will search through a space one cube larger in every dimension (21x21x21 for my input), starting with one of the corners using the breadth-first search.
Links
The repository with a complete solution (including input parsing) is available here: https://github.com/HappyCerberus/moderncpp-aoc-2022.
I post daily Modern C++ content on Twitter, LinkedIn, Mastodon, Medium and Substack.