by Kristian Rother
This cheat sheet was originally prepared for a talk at PyLadies Hamburg.
Advent of Code is an advanced coding challenge.
- one challenge released at 5:00 a.m. UTC per day December 1st - 25th
- every challenge has two parts
- there are >100k participants globally
- collect up to 50 stars (two per day)
- the challenges come with great santa stories
- rehearse coding fundamentals
- understand requirements
- decompose problems into smaller parts
- estimate computational complexity
- programming practices (OOP, functional programming)
- code cleanup, refactoring
The biggest step towards the solution is choosing a good data structure.
All problems can be solved with standard Python data types: list
, dict
, set
Data structures that you will need for some of the challenges are:
- stack
- queue
- chained list
- tree
- graph
- 2D array
Identifying a good algorithm is essential. For many challenges a brute-force solution is computationally infeasible!
Here are some algorithms that I have used in AoC with links to according challenges:
- Move along a 2D grid
- Crypto mining
- Graph traversal
- Interpreting an assembly-like language
- Conways Game of Life
- Josephus Problem
- Dynamic Programming (backpack)
- 2D Convolutional Kernel
- kd-Tree
- Dijkstra
Most challenges use strings and integer numbers only. You do not have to worry about floating-point precision, probabilities and heuristic algorithms.
To learn more about algorithmics, I recommend Algorithms by Robert Sedgewick and Kevin Wayne.
Some Python libraries I have used over and over in AoC:
re
– essential to parse most inputsnumpy
– fast arrays help with many challengesdeque
– fast queue implementation (much faster thanlist
)heapq
– for priority queuesitertools
– iterable helpers like permutations, products etc.
Careful with deepcopy (slow)
I made heavy use of automated tests using pytest
:
- test a minimal input first
- copy-paste the small examples from the AoC page, write tests for them
- test your toplevel functions only unless it gets really difficult
- add more examples to cover boarder cases (use
@pytest.mark.parametrize
) - great opportunity to exercise TDD (see live TDD in Uncle Bobs "Clean Code" lecture
AoC works great in teams with:
- pair programming
- people using different programming languages
- beginners who spend their time researching stuff
- solving challenges of a previous year at a slower pace
time | what I do |
---|---|
5:50 | get up |
5:55 | coffee |
5.59 | power-click refresh button |
6:00 | GO! |
9:00 | call with AoC team (30' timebox) |
9:05 | review solutions of previous day |
9:20 | exchange ideas for current day |
? | solve the challenge after 10 minutes to five days |
Doing this for 25 days in a row is physically and mentally exhausting! You can hurt yourself!
A better idea might be
- Go slow and steady.
- Do not try to go as fast as possible.
- Do lots of refactoring and code cleanup
- It is OK to stop at any point.
- If you solve one problem, be proud of yourself already.
You can find my solutions on www.github.com/krother/advent_of_code.
(c) 2022 Dr. Kristian Rother
This Cheat Sheet is available under the conditions of the Creative Commons Attribution International License 4.0
The code on www.github.com/krother/advent_of_code is available under the conditions of the MIT License. See LICENSE.TXT for details.