Krzysztof Witczak

Advent of Code

December 29, 2023

I discovered Advent of Code two years ago when one of my colleagues told me about it on our 1:1 (hey, Pedro! 👋). Starting from the 1st of December, Eric Wastl publishes a coding riddle on his website every day for the next 25 days. The rough idea is the following:

  • Every participant has the same text riddle. They are themed as a crazy Christmas story, where you need to help elves save the day. This time around you need to solve a “lack of snow” crisis!

Elves

  • Every riddle has connected text input that you need to process.
  • Every input is uniquely generated (or there is a finite number and they are randomized, I’m not sure) which leads to riddle answers also being different for participants. It is forbidden to share those inputs online.
  • Every riddle also has a small example input and answer to it to check your solution against. It’s often dozens or hundreds of times smaller than your generated input. Great for testing!
  • After providing the correct answer, you get a star ⭐️, and then you’ll see Part 2 of the riddle, which changes the task and requires a new answer. Once you get the second part right, you get another ⭐️. As a result, it’s possible to get 50 ⭐️ in total.
  • Website records when you submit an answer. Based on the elapsed time since the riddle was published, leaderboard of the fastest participants is created.
  • You can also create a personal leaderboard where you can compete with your friends.

Every day completed by you draws a cool image to motivate you to push further!

AoC main page

You can read more about the general idea on the website about section.

Difficulty

The first days are usually simple enough to be completed in less than 10 minutes by top performers, and it’s reflected on the leaderboard. For developers who rarely do similar tasks, it may take much more, but still, they should be simple enough to finish in the morning before your job starts (at least in Poland!).

However, difficulty quickly ramps up. There are usually two main drivers:

  • Every week it gets tougher, besides the last day… 😇
  • Usually riddles are more difficult on the weekends.

One of the Redditors (xHyroM) made a website which displays how difficulty increases based on average times on the leaderboard, comparing multiple years.

AoC Difficulties across the years

The trick is that part 2 of each day usually forces you to refactor your solution. An example may be where you implemented part 1 and it computes in 2 seconds, but then part 2 introduces a small difference which increases the number of possible combinations to check 1000 times. As a result, your solution for part 1 would compute for more than a day… 😄

Here what often happens is that people optimize their brute force approach (which is often enough), while others use clever algorithms to solve riddles using computer science or mathematics.

Examples of lessons learned

Sometimes it’s better to work on ranges, rather than arrays. It was my mistake in the past to look for overlaps between sets of data and use arrays to hold that data. For example, consider this code:

a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]

Your task is to check if they overlap. In ruby, you can easily get this answer using methods like #intersection. If you have 1000s of such arrays it may get a little slow, but what if each of them contains millions of elements? It will get very slow. In reality, you don’t need an array, because you only care about the edges of the data structure, contents are not important! Ruby has a perfect class for this and its range. You can transform it to array if needed, but if it’s not necessary, you can operate only on what’s important to you.

(1..5).overlaps?(4..8)

Another simple example may be that sometimes computation repeats in equal cycles. Often you can cache the cycle result and use it on the next iteration, instead of computing once again. Sometimes you need to act with gigantic numbers, while their size is just a distraction, and you can reduce them using LCM (least common multiple).

Additionally this year I’ve learned about PIP - point in polygon which is a fairly simple algorithm and it helped me solve one of the puzzles. The trick was to realize that we can interpret the thing as a polygon in the first place! 😉

I only reached day 17 before my chores stopped me from doing AoC in mornings before job, but I’m pretty sure that in the next days I’ll need to apply Dijkstra’s Shortest Path algorithm or BFS (Breadth-First Search). I’m not aiming at finishing all 25 tasks tough, I believe the principle of “do it as long as it’s fun” is healthy approach.

Community

One of the amazing things about AoC is the people around it - there is a huge reddit community with 118k members, discussing every riddle, providing hints, and approaches they took, asking questions.

But not only that! Because AoC has existed for years now, some people make it even harder for themselves and they use it to learn a new language or even challenge themselves to solve every day with a different language! It’s also almost a tradition for the most ambitious participants to create awesome solution visualizations!

Summary

AoC is amazing. Eric Wastl did an amazing job once again, and created a huge fan base all across the globe - both very skilful and experienced, as well as newcomers or even managers like myself or Will Larson, who have fewer opportunities day-to-day to work on coding puzzles but still love it, and with Advent of Code it’s easy (and seducing!) to come back for a couple of days or weeks to feel that thrill again.

Additionally, if you want to know even more about the entire event, watch work behind the scenes…

Many thanks, Eric! 👏