#36: Advent of Code
Happy Monday! Welcome back to The Coder Cafe! As this week marks the beginning of the Advent of Code, I wanted to explore the topic of coding challenges.
The Advent of Code (adventofcode.com) is by far my favorite coding challenge. It’s an advent calendar of programming puzzles. Every year since 2015, a new problem is posted every day in December until day 25. Each problem consists of two parts, and we can earn a star for solving each part (the second part is an extension of the first one), allowing us to collect up to 50 stars per year. In general, the puzzles get more difficult over time.
Over the years, the Advent of Code has grown significantly in popularity, reaching in 2023 almost 250,000 participants who managed to earn at least one star.
What I love about Advent of Code is how heavily the problems focus on algorithms and data structures. Personally, it has helped me a lot to strengthen my skills in these domains, which I used to view as boring in the past—especially because that was something that I had to study for coding interviews, not something I wanted to. But thanks to Advent of Code, I have come to truly enjoy these topics. Working on real problems made all the difference for me.
To make it more concrete, here’s a problem example, a bit similar to the Game of Life. We’re giving a board with different kinds of elements: #.|
.
.#.#...|#.
.....#|##|
.|..|...#.
..|#.....#
#.#|||#|#|
...#.||...
.|....|...
||...#|.#|
|.||||..|.
...#.|..|.
This board comes with rules. For example, if there’s a #
with at least three .
around, then the #
will be replaced by a .
. For example, after one step, the board will be the following:
.......##.
......|###
.|..|...#.
..|#||...#
..##||.|#|
...#||||..
||...|||..
|||||.||.|
||||||||||
....||..|.
In part one, we have to calculate the state of the board after ten steps. So, we implement the rules in the language we want, we loop from one to 10, and we can collect our star. Yet, for part two, we have to calculate the state of the board after 1,000,000,000 steps. Clearly, looping from 1 to 1,000,000,000 doesn’t work anymore, so we have to come up with a creative solution to solve the problem.
This is another aspect I enjoy with the Advent of Code; it pushes me to think outside the box. I can’t count the number of problems that I faced thinking: “It’s absolutely impossible!“. Yet, it’s always solvable, and it is a great way to spark creativity.
Another cool aspect is that we can solve the problems in the language you want. We’re given a file for each problem containing the input, and what we have to produce is an output, not a source code. If the output is valid, we get one star; if not, we have to revise our algorithm:
Giving the option to solve the problem in any language is a great idea, and many people leverage this opportunity to learn new languages while doing the Advent of Code. For example, a few years ago, I started learning Rust thanks to the Advent of Code, and last year, I started practicing Haskell with the first challenges as well1.
So, if you haven’t already, why should you give the Advent of Code a try? It can help you to:
Practice concrete algorithmic problems
Brush up or strengthen your data structure knowledge
Improve your problem-solving and optimization skills
Come up with creative solutions to non-trivial challenges
Learn and practice a new language
One last thing I wanted to mention is don’t be too hard on yourself if you’re struggling and can’t complete some problems. I completed all the Advent of Code, but as I shared elsewhere, in 2016, I only managed to collect a few stars. It’s all about practicing, and if you’re having fun, you will progress even more!
Tomorrow, we will explore a challenge that can help us learn about writing idiomatic code in a new programming language.
I quickly stopped when the problems became too difficult, though, and switched back to Go.