Things I'm (re)learning as I play Advent of Code 2023.
One of the best coding challenges out there is Advent of Code. It is a really fun and engaging way to learn a new language or to revisit something that you've been away for so long. In my case it was python. Python was the language I wrote my PdD in, but at the time I was using version 2.7. I barely touched python since. This year I decided to do AoC with python 3.11 and wow, what a difference. I love this language. It is so pleasurable to work with.
I'm keeping this entry about the things I'm (re)learning1. Thing that either I didn't use before or were still absent in 2.7 days. These things emerged as I progressed through the challenges and compare my code with others2.
Around day 18—20 of AoC I stopped participating. Challenges became more specific about using certain advanced algorithm or data structure and became harder. As a consequence the time devoted to solving increased. The issue is that I don't have that time, but in any case I'm still going through solutions. The objective is to learn and recall the most of python possible. And in the end it is all time management.
Tips learnt
enumerate
is a very useful command.zip
is also very useful.- Prepare the input data by chaining everything with appropriate open..read..strip..split.
But sometimes numpy is easier with a simple
loadtxt
. - Jupyter Labs has come a long way. I never liked it back then. Now either via the browser or in VSCode, interactive python is a breeze.
- Many times list|dict|tuple comprehensions make the code very clean and easy to read.
I'm thinking twice about the
for
loops that I'm trying to implement. - Type annotations are very useful, even for small scripts, as they force you to decide on what you want to write.
*[]
and[::1]
- Many things are easy if I leverage
numpy, re, math
and don't try to reinvent the algorithms. functools.cmp_to_key
works but I still find it a bit cumbersome to use. There should be a cleaner way to sort by user-defined comparators.numpy.where(condition)
in 2D matrices gives two arrays with the indicies of the entries. Better usenumpy.argwhere(condition)
and.tolist()
. The former case needs a lot of massaging to get a tuple of coordinates back3.scipy.spatial.distance
has methods for spatial distance calculations that are very useful, namelypdist
,cdist
, andsquareform
splitlines
can shorten the loading of the inputs... something in the line ofopen(0).read().strip().splitlines()
to read from standard in.
Things to explore in the future
- The Shoelace formula, gets the area of a simple polygon from its coordinates. I think I'll need to use this on a urban agent based model I'm running now. As a follow up to this, the Pick's theorem is also important.
numba
This is exciting. Never touched it before, so I'm eager to look into it when I have a bit more time.- Go deep with
numpy
. - Day 12 pointed in the direction of recursion. I struggled with this exercise. Recursion is something that everybody understands until you really have to apply it to a new problem. Need to revisit this again. Not submitting any answers as I feel I didn't solve it.
1 And we are only at day 20 at the time of writing. I'll update this as AoC progresses.
2 My AoC repository contains my daily solutions in case you want to check my progress.
3 https://numpy.org/doc/stable/reference/generated/numpy.argwhere.html