Advent of Code

October 7, 2024

For the last two years, I participated in Advent of Code, an Advent Calendar with a new programming puzzle every day.

For the second time, I found it a very rewarding experience, fun and also educational, and I would like to share some of that.

Learning new concepts

For every day, I wrote down some notes to the problem, how I tackled it, what my struggles were and the different approaches I tried. You can find those in the README to my last year's AoC code.

I learned a lot, like writing my own Dijkstra algorithm (and getting a deeper understanding), or using Memoization. Also, LCM came in handy at leat two times.

One thing that stuck out to me because I'd never heard of it was the Shoelace formula: If we have the coordinate pairs of a 2d polygon, we can easily calculate its area. For example, for the following polygon:

we can calculate the area in with the shoelace formula:

Credit: Wikipedia

Being part of an active community

Solving the puzzles is one part, but the exchange with others (and seeing other people having the same struggles) was a big part of the fun for me. In December, the Subreddit /r/adventofcode is very active, and everyday, memes are created and shared, and hints to the puzzle of the day can be found. I limited myself to only browse the Subreddit if I already solved the puzzles on that day (because of Spoilers), which went pretty well for most days.

For example on Day 25, there were multiple approaches that would work - visualizing the problem with for example Graphviz would reveal the answer pretty quickly:

Memes on the Subreddit:

Optimizing code

For my daily work, the main focus of the code is to keep it readable, tested, maintainable and generally in a good shape (refactor now and then to avoid technical debt). It is great to have an opportunity at AoC to really tinker with code and try something new (and sometimes also fail!), just to make it run faster.

For example, near the end (when the more complex problems tend to appear) on Day 23, we were faced with a pathfinding problem, to find the longest path (without loops) inside of a maze, which is a hard problem in general.

Using DFS I had a working solution, but it took around 2-3 hours to run. I then switched the data structure to a simpler graph by contracting paths where there was only one way to go, and got it down to around 15 seconds. There would still be room for improvement, but getting it down to a reasonable runtime (<1min) was the main goal for me for all the puzzles.

Try out new languages

In 2022, I decided to give Kotlin a try, also because there was a competition by JetBrains, where everybody which solved at least a couple of days was participating in a draw for some prizes.

For the last AoC, I decided to have a look at Rust, just for the fun of it. (There were also people who randomly chose a language each day or which solved every day in a different language).

I was actually suprised to learn that Rust as a rather low-level language supports many concepts for iterating and functional programming, which was nice to learn.

For example for Day 9, my code looks like this (full code here):

fn solve(data: &str, backwards: bool) -> i32 {
    let mut total_sum: i32 = 0;
    for line in data.lines() {
        let mut numbers: Vec<i32> = line.split(" ").map(|s| s.parse().unwrap()).collect();
        let mut current_nr: i32 = 0;
        let mut mult: i32 = 1;
...

Here are the runtimes for the 3 days that I did in Python, Codon (compiled Python) and Rust:

Day Python Codon Rust
Day 6 3.0s 253.1ms 21.3ms
Day 9 40.4ms 3.8ms 505µs
Day 15 51.8ms 3.6ms 590µs

For every Day, the step from Python to Codon and also from Codon -> Rust is around one order of magnitude faster. It was quite satisfying to get a runtime of something like "505µs", something which I'm definitely not used to in my daily work.

Enjoying the Advent Calendar

Every year, Eric (the inventor of AoC) invests a lot of time into the calendar main page, and every day unlocks a new part of it. It's great to see your own progress visualized, and most of the time it fits the Story behind the calendar. For example for 2015 (which I continue a bit now and then for practice) looks currently like this for me:

Participate together

I also found it great to participate together, some co-workers joined in and it was very interesting to hear about different approaches and thoughts. You can even have private leaderboards if you want, to see who has solved which day.


Overall, I really enjoyed the AoC experience. You can find my solutions here, github.com/akleemans/aoc-2023

There is also a great "Behind the Scenes" video from Eric which I can really recommend: https://www.youtube.com/watch?v=Mb8WwKS6ajk

Happy coding and maybe see you at AoC in December...?