Advent of Code 2020 Review

Today I am talking about the Advent of Code 2020 and what I have learnt doing it this year.

Advent of Code 2020

Advent of Code is a set of programming puzzles with one problem released each day during Advent. Each problem has two parts, the second normally being an expansion on the first.

For most of this year, the second problem expands the first by making it infeasible to brute-force the solution and requires clever algorithm design or datastructures. All problems should be able to run within a few seconds as long as your solution is somewhat efficient.

Like last year I participated in the Advent of code and thoroughly enjoyed it. This year it provided a similar level of challenge while keeping the problems interesting and fun to work on.

This year two of the highlights were implementing a linked list (not something I have done since University) and learning about the Chinese Remainder theorem.

Implemented a linked list

One of the problems required moving segments of an array around and inserting it into other positions in the array. Originally I was using an array and re-composing it every time I needed to slice and move the elements. This worked for the smaller part one but for part two it was too slow.

In addition to the slowness of re-creating the array, part of the problem required you to find a specific entry in the array. This meant that you were a O(n) search every loop iteration which was slowing down my solution.

Initially I was going to use a Java linked list however I encounted some issues with it. The main one was that it isn’t a “true” linked list implementation. Since it is generic it doesn’t provide any methods to move the tail pointer to another element and “quickly” alter the list.

By implementing a simple linked list this allowed me to have custom methods to slice out a set of elements and insert them anywhere just updating the tail pointers of the involved elements.

It was a fun little challenge which sped up the problem considerably and reminded me of the benefits of understanding your data structures.

Chinese Remainder Theorem

The Chinese remainder theorem is not something I had previously read about in my number theory work. For one of the days it was noted as a possible solutions instead of brute forcing the solution (which depending on your solution might take days). My initial solution unoptimized solution was going to take a couple days to solve the problem.

Reading up on the theorem proved interesting as it was able to be used as Advent of Code had helpfully ensured all numbers were pairwise coprime. This allowed solving it relatively quickly as it is one of the requirements of using it.

As with all Advent of Code problems, using this required a little manipulation of the problem but once done the solution was pretty quick to implement.

Personal Stats

This year I managed to finish most puzzles by the end of each day. Over time as the puzzles increased in complexity I was no longer able to complete them before work and instead did them during lunch or after work.

Part 1Part 2
DayTimeRankTimeRank
25>24h12028>24h8983
2409:40:33930610:12:328098
2316:27:381184218:39:338776
2215:28:391400618:07:5611522
21>24h13068>24h12838
2011:03:32816115:06:204551
1912:24:151041514:49:248442
1813:05:281508618:10:5815560
1713:36:031438514:08:1813796
1612:48:241987216:34:4017374
1514:41:002259214:42:5720548
1403:54:351075915:23:4318138
1307:43:352045310:33:5912396
1205:49:531521506:47:2013700
1103:39:101174004:06:219322
1003:25:261756612:32:5821742
902:54:491560003:04:2314076
804:16:492070305:10:4418689
705:25:591747305:51:4114514
603:59:151888404:04:3017054
505:15:302099405:18:3119532
403:40:472067803:59:2014769
303:46:472102203:53:1319277
202:36:211432102:42:4713419
116:49:516190916:53:5757241

Final Comments

Again this has been an incredibly enjoyable experience working on the problems. Part 1 is normally simple enough to quickly code a solution which part two typically requiring optimisation of your solution.

I highly recommend trying Advent of Code as the puzzles are novel and allow you to learn about optimisation of problems. Part one is normally simple enough for someone learning to program and then offers

In addition to being fun, the community around Advent of Code also are very happy to help out and discuss the problem. This means you can read about other peoples solutions and get help if you are stuck.

All my code for this year and previous years is on Github so feel free to have a look through how I solved the problems.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.