New blog post: Advent of Code in C++ Template Metaprogramming
Long story short: I wrote up a solution to Advent of Code day 1, using C++ template metaprogramming.
Historically I haven't usually done Advent of Code, but this year a few friends were talking about, and so I started pondering: Are there any new or esoteric languages that might make fun exercises if I solved AoC in them, as a way to learn or brush up on them?
… I was then attacked by a stray intrusive thought proposing: What if I did it purely in compile-time C++ using template metaprogramming????
I briefly looked into the problem, and, after a day or two of work, concluded it was probably unworkable, and that processing a 20,000-character string using C++ templates just couldn't be made performant.
But, the idea was stuck in my head, and a little bit later later I found myself poring over cppreference, looking for hacks I might use to improve performance … and a few hours later I had a working solution to Part 1.
Part 2 was harder; I wrestled with bugs in my state machine for hours, and eventually had to resort to:
- Writing a solution in Python, and comparing the two versions, and
- Recruiting my wife Kate to stare at my state machines with me; she ultimately spotted my missing transitions before I did.
I'm not completely happy with the writeup -- the solution is definitely presented as "handed down from God" over writing up the journey, and I actually think the "fast compile-time type-level fold" trick is neat and doesn't high enough billing; but I wanted to get something out while Day 1 was still recent and relatively fresh in the zeitgeist, and I think I succeeded at that.
Hope you enjoy.
And, as always, the reward for being a newsletter subscriber is a Ranger photo. He found a palm frond!
- Nelson