Daily bit(e) of C++ | Advent of Code: Day 13
Daily bit(e) of C++ #346, Advent of Code using Modern C++: Day 13.
Welcome to day 13 of Advent of Code using Modern C++. Before you start reading, I highly encourage you to try to solve the two problems yourself: https://adventofcode.com/2023/day/13.
If you missed it, I have prepared a template repository for C++, with a development environment (through a devcontainer) with a bleeding edge version of the C++ toolchain: https://github.com/HappyCerberus/aoc-2023-cpp.
Today, we are tasked to find mirrored sections of maps.
Part one
Comparing strings (especially in column order) is cumbersome. We can greatly simplify the problem by representing each row and column as an unsigned integer (treating ‘#’ as 1 and ‘.’ as 0 bits).
To find a mirrored prefix of an array of integers, we can iterate over all array splits into a prefix and suffix (with the prefix mirrored) and compare the prefix against the suffix. We have found our mirror line if we do not find a mismatch besides the length.
We can apply the above logic to rows and columns and then fold over all notes, to sum up the values.
Part two
The second part is described in a fairly confusing way, but what we are trying to do is to find a mirror line that produces exactly one bit-error.
Instead of looking for equality, we can count the number of bit-errors, and if we find a split that produces one error, we have our new mirror line.
Conclusion
How did you do with today’s problems? Did you also simplify the input?
Share your solutions and insights. Check out the repository with solutions and links to all articles: https://github.com/HappyCerberus/aoc-2023-cpp-solutions.