Modern C++ in Advent of Code: Day 25
The final day of Advent of Code is here and we are trying to feed some SNAFU to Bob.
I do encourage you to try to solve it yourself first https://adventofcode.com.
Input
Today’s input is a list of numbers in SNAFU format; we will take it as std::vector<std::string>
.
To int and back
Our goal today is to write conversion functions from and to the SNAFU format.
Converting from SNAFU is straightforward as we can treat it the same as any text-encoded number, increasing the exponent as we parse the number back-to-front.
Converting to SNAFU is a bit more tricky.
First, let’s consider the range of numbers we can reach if we put a specific digit, let’s say 2
, at the front of our number. The maximum number will have the pattern 222…
, and the minimum will have the pattern 2==…
.
So when should we pick a specific digit? Well, when the number we are trying to represent is within this range. Following this idea, we can now construct our conversion, checking the low boundary for each digit.
And with that, we are done with Advent of Code 2022. Thank you very much for joining me on this journey. Regular content will resume on 2nd January 2023.
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.