PROGRAMMING

How To Save Christmas by Coding: Advent of Code

Join the challenge and improve your programming skills during the holiday season.

Fran Sandi

--

A laptop in a christmas setting
Photo by Joshua Aragon on Unsplash

I recently talked with a friend who was very excited about the end of the year because of the Advent of Code. I had no idea what he was talking about, but then he explained that it’d been a yearly programming event since 2015. Fifty new puzzles are released between December 1st and 25th — similar to a traditional advent calendar. It’s a huge event, with thousands of programmers competing to reach the leaderboard and multiple Reddit discussions about their solutions and approaches.

I have enjoyed coding challenges and competitions since I started taking my first steps in programming. In my experience, solving problems that require knowledge about algorithms and data structures is the best way to learn and practice essential programming concepts that are not often used in the day-to-day work of a developer. It’s also an excellent way to have fun (if you enjoy coding, of course) while learning and sharpening your programming skills.

I decided to try this challenge and went for last year’s puzzles. It caught my attention immediately and became my hobby for the past few weeks. Now I’m as excited as my friend, waiting to participate in this year’s event.

What is the Advent of Code?

According to Eric Wastl, the creator and maintainer of the challenge:

“Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as a speed contest, interview prep, company training, university coursework, practice problems, or to challenge each other”. (source: Advent of Code website)

Participating in this challenge is free; you can support it by sharing it with others (what I intend with this article) or donating to the project.

Advent of Code Logotype
Advent of Code — by Eric Wastl

My experience in the 2021 challenge

The beginning of the journey

I started my way through the challenge by opening the 2021 event, and I found the following mysterious message:

You’re minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!

Before you know it, you’re inside a submarine the Elves keep ready for situations like this. It’s covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there’s a little meter that indicates the antenna’s signal strength by displaying 0–50 stars.

Your instincts tell you that in order to save Christmas, you’ll need to get all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

Ok, so now the rules were clear. Every day consisted of two puzzles I had to solve, collect some stars the Elves needed to improve the submarine’s antenna signal strength, find the sleigh keys, and save Christmas. It seemed important enough to me, so I didn’t lose more time and started with the first puzzle right away.

About the puzzles

I’m not going to spoil the rest of the submarine’s story or go into the specifics of the coding problems, but I can say that the difficulty of the puzzles started very low and then grew as the days passed. The last six days (12 puzzles) were the most challenging, as they required a lot of thought and some research about algorithms and math concepts to make my solutions efficient enough.

Something that makes this event very different from other programming challenges I participated in is that your code is not analyzed at any point to judge your solution; only your final output is checked. This means you have more flexibility to use any programming language you want or even solve the problems by hand (please don’t even try that). In some puzzles, you could end up with a brute-force solution that still does the job. This doesn’t mean that performance is irrelevant; for many exercises, an inefficient approach would take too long to execute, and you wouldn’t have the output on time to help the Elves.

The puzzle topics were varied, as they covered some programming concepts such as Arrays, Queues, Binary Trees, Heaps, Graphs, Dictionaries, Sets, Recursion, and Dynamic Programming, among other data structures, algorithms, and advanced math concepts.

In the end, I was able to finish the puzzles and get all 50 stars, which was a good exercise for my programming skills; especially helpful in remembering Python syntax and some libraries usage since it’s not a language I use very often, but the one I chose for the challenge.

The 50 stars of 2021 challenge
The 50 stars of 2021 challenge

My solutions

If you’re interested in my solutions in python3, check this repository.

Even if I’m publicly sharing my solutions, I strongly encourage you to attempt to solve the puzzles yourself before checking someone else’s approach. In my experience, it’s more beneficial (and fun) to do it that way.

My coding structure

I recently decided to start sharing more of what I do (more about this in my article “Why You Should Share What You Are Learning and Working On) which is one of the reasons I’m writing this post and also why I decided to share a repository with the code structure I used during the challenge.

This code structure is for you if you plan to use python3 for the challenge. The project already includes the example input/output for all 2021 puzzles so that you can test your solutions easier during development. It also contains the basic structure for the upcoming 2022 challenge. I will update it when more puzzles are released, but it can be used for any past or upcoming year. Feel free to use this repository as it best fits your needs.

Advent of Code 2022

After my experience with the puzzles from the 2021 challenge, I’ll definitely participate in 2022 and try my best to help save Christmas again.

If you’re interested in joining this year’s event, it will start on December 1st, and as you already know, two puzzles will be published every day until December 25th. You only need to sign in to the Advent of Code website to start. Don’t worry if you don’t have enough time to participate every day or if you decide to join later since once the puzzles are released, they will stay on the website indefinitely, and you can catch up later.

Have a happy Advent!

If this article somehow motivated you to join the challenge, the resources I shared were valuable for you, or you have any thoughts to share with me, leave a comment on this article or contact me on Twitter so we can keep sharing and learning together.

Follow me on Medium if you’re interested in more articles like this.
I usually write about my recent learnings, my work as a software engineer, experiences, and tech-related topics.

Useful links:

--

--

Software Engineer @ The Washington Post / Ex-Amazon | 🚀 Entrepreneur | 👨‍💻 Developer | 🧩 Puzzle solver