Every November the same question turns up in every forum I still bother to read, and every year I feel a small, irrational flicker of irritation before I answer it. Which language should I use for Advent of Code?
Wrong question. Mostly.
I'll explain why it's wrong, and then, annoyingly, I'll spend a couple of thousand words explaining why it's also a little bit right. The puzzles unlock at midnight US Eastern, which is five in the morning on the Portuguese clock. That single fact filters people out far more aggressively than any language choice ever will. By the second week, the person still standing is not the one with the cleverest type system. It's the one who went to bed at a sane hour on December 6th, when day 6 looked easy and day 7 was going to be easy too, and who therefore had something left in the tank for the first genuinely nasty puzzle. I learned this the hard way, and I learned it more than once, which says something about how well I learn.
So let me get the unfashionable verdict out of the way early, because I don't want to bury it under a performance of balance. For most people, most years, the answer is Python. Not because Python is beautiful or fast or because I'm fond of it. I have complicated feelings about whitespace-significant code after a decade of debugging other people's indentation. The answer is Python because of what the puzzles actually are.
People imagine Advent of Code as 25 algorithm problems wearing a Christmas costume. It isn't, quite. If I'm honest about where my hours went, something like 40% of the time on the early days was parsing. Reading the input, splitting it on blank lines, pulling integers out of a sentence like "Monkey 3 throws to monkey 7", converting a grid of characters into something I could index. That's string wrangling, and string wrangling is where Python, Ruby and the old Perl crowd have a structural advantage that no amount of language-design prestige can erase. A one-line `re.findall(r"-?\d+", line)` does the work that takes a small ceremony in most compiled languages. You write it, you forget it, and it never once fails you at 5:20 in the morning.
And there's the grid trick. If you've never stored coordinates as complex numbers, try it once. `z = x + y*1j`, and now direction changes are multiplication by `1j`, neighbours are four additions, and a dictionary keyed on `z` is your whole map. It feels like cheating. It's the kind of thing that only a language with a cheerful disregard for purity would let you do, and I've shaved real minutes off real days with it.
Now. The counterargument, which I've heard a hundred times and which has some force: Python is slow, the puzzles get hard, and eventually you hit a wall where you need speed.
Honestly, this is true about 8% as often as people think.
Eric Wastl has said, more or less, that every puzzle has a solution that finishes in around fifteen seconds even on old hardware. The inputs are designed so that the naive approach dies and the right approach lives. That means the thing separating you from the star is almost never constant-factor speed. It's whether you noticed the trick. Part two of the day is nearly always the same puzzle with a number that has grown by several orders of magnitude, a lovely cruel moment where your brute force from part one quietly sets your laptop fan on fire.
Take 2018, day 9, the marble game. Part one is a few thousand turns. Part two multiplies the marble count by a hundred. In Python, if you do the obvious thing and `list.insert` into the middle of a growing list, you're paying a linear cost per insertion, and it grinds. I sat there watching it for what felt like a long time, maybe four minutes, before I killed it. The fix is `collections.deque` and its `rotate` method, which turns the circle into something you spin rather than something you search. Runs in about 1.1 seconds. The language wasn't the problem. My mental model of the data structure was the problem, and a faster language would have only made me wait less before admitting that.
Then again. I should be careful, because I'm about to contradict myself and I'd rather do it on purpose.
There are days where raw speed does buy you something. 2020, day 15, the memory game, where you play out thirty million turns. Straightforward Python, using a dict from number to last-spoken turn, took me around nineteen seconds under CPython. Under PyPy it dropped to roughly 3.4. A decently written Rust version finishes in a bit under a second, though I'd note you can write the Rust badly and get the same ballpark as PyPy. So yes, speed matters there, and nothing about the algorithm can save you, because the algorithm is already the right one. It's just a lot of turns. Nineteen seconds is irritating. It is not fatal. You get your star. You go to work. The only people who care about the difference between 19 seconds and 0.9 seconds are the people optimizing for a leaderboard or a bragging post, and I'll say something about those two groups in a minute.
Let me talk about Rust first, since it's the fashionable answer now and I've done it, and I'm not sure my conclusions are the ones its enthusiasts would like.
In 2021 I decided to do the whole thing in Rust. My reasoning was the usual self-improvement reasoning: this is a good excuse, small problems, low stakes, I'll learn the language properly. And I want to be fair here, partly. It worked, eventually. But the first fifteen days cost me, by my own rough tally, about 1.6 times what Python would have, and I'm including days where the puzzle itself was trivial. The tax was never the algorithm. It was `error[E0502]: cannot borrow as mutable because it is also borrowed as immutable`, over and over, usually when I was trying to update a grid while reading from the same grid, which is something roughly every third puzzle wants you to do. And then the runtime classic, `thread 'main' panicked at 'attempt to subtract with overflow'`, which is what you get when you compute a neighbour of cell zero using `usize` and subtract one. You find out about this at exactly the moment you least want to learn anything.
By day 20 or so I had built myself a little `Grid` struct, a parsing helper, a few iterator adapters. And the later days got faster, a lot faster, and the code ran in single-digit milliseconds, and I felt, I'll admit, a childish pleasure watching the timings scroll by. So did Rust "win"? I don't know. If I count the hours honestly, probably not. If I count what I came away with, maybe. This is the part where I'd normally tidy up the argument and I'm going to refuse, because the truth is that it worked only because I'd already decided the leaderboard didn't matter to me. If I'd cared about speed of solving, Rust would have been a self-inflicted wound.
Which brings me to the leaderboard, as promised.
If you look at who lands near the top of the global board each year, there's an uncomfortable amount of Python in there, plus people with prepared libraries and absurd typing speed, plus a handful of folks running very custom setups. The best of them have been at it for years and have, in effect, a private standard library for AoC: a grid class, a Dijkstra, a function for parsing ints, an `Intcode` interpreter that they wrote in 2019 and now treat like a utility. The language is almost incidental. What they've really optimized is the distance between reading the puzzle and having code typed. I personally never got anywhere near it. My best finish on a good night was somewhere around rank 1,400, which is a number I remember with more affection than it deserves, and then I went back to bed.
Chasing the leaderboard is a legitimate hobby. It's just a different hobby from the one most people say they're doing when they ask me about languages. They say they want to learn, and what they mean is that they want to feel like they've accomplished something before Christmas. For that, almost nothing beats picking the tool whose friction you've already stopped noticing.
I keep saying "the language you already know" and I realize that sounds like a cop-out, so let me be less polite about it. The single worst reason to choose a language for Advent of Code is that you want to learn it. I've watched this fail in a dozen ways. You choose Zig because it's the one people are posting about. Or Mojo, which I'd bet money has a pile of abandoned December repositories somewhere. Or you do the "one language per day" thing, which is a fun stunt for a video and an awful way to solve a puzzle. Learning a language and solving a puzzle are two different cognitive loads, and puzzles come with a clock and a streak and a faint social pressure that makes you stupider than usual. You stack a second heavy load on top and by day 9 you're not learning anything, you're surviving.
I did say that, didn't I. And I'm about to undermine it.
In 2020 I did a good chunk of the month in Haskell, a language I had no business attempting. I abandoned it around day 19, a couple of days after the monster-message regex puzzle, where I'd written a parser combinator that was, I'm now fairly sure, wrong in an interesting way. And yet that month taught me more about parsing than the preceding five years of writing regexes. It taught me to think of input as a grammar rather than a string. I still do that. So here's a position I can't entirely defend: don't pick a language to learn it, except that sometimes the best learning of your life comes from doing exactly that, as long as you're willing to let the month collapse. The skill is knowing in advance which kind of year you're having. I never know. I find out around day 11.
Different matter, while I'm in the neighbourhood of old mistakes. For about two Decembers I kept a handwritten log of how long each part took, on an index card taped to the monitor, and by the second year I'd stopped trusting the card because the numbers were so obviously flattering. Never mind. That's not the point and I don't have a point.
Back to the thing that I think actually decides outcomes: the REPL.
When you're in the middle of a puzzle and you don't know what the data looks like, the fastest way to find out is to poke at it. Load the input, look at the first five lines, check whether the grid is square, find out whether there are negative numbers hiding somewhere. A fast interactive loop is worth more than a fast compiler. That's why Python with IPython or a notebook is so lethal for this, and why Clojure people and Haskell people with `ghci` open all month do better than their language's reputation suggests. It's also the part Rust makes slow. Compile, run, stare, edit, compile again. Each cycle is only a few seconds, but at five in the morning the seconds have a way of becoming the whole experience.
Related: the tooling around the language beats the language. I used to start each December by copying last year's folder, which meant that by day 3 I had two stale inputs and a broken path. Now I have a template: one file per day, a function for part one and one for part two, a tiny harness that reads `input.txt` and the example input and prints the elapsed time so I can see that dreadful 19-second moment coming. I fetch the input with a script that uses my session cookie, caches it locally, and sends a sensible User-Agent, because the site asks politely that you not hammer it and I think that's a fair thing to ask of anyone who is being given free puzzles. There's also a Python package, `advent-of-code-data`, that does the fetching for you. I never adopted it because I enjoy owning my own small mess, which is a character flaw and not a recommendation.
On that note, the example input. People skip it. Please don't. About a third of the wrong answers I've ever submitted were wrong because I solved the example by accident and the real input had a case the example didn't. The other two thirds were off-by-one errors, which are language-agnostic and eternal. The first thing I do every day, before I think about algorithms, is write a test that asserts the example answer. Slow? A bit. It saves me, conservatively, a few wrong submissions a week, and the site makes you wait between wrong submissions, so the saving is real time, not just pride.
Let me say something nasty about a few of the other candidates, since I'm supposed to have opinions and I've been far too even-handed in the last few paragraphs.
Go is fine and boring. People who pick it for AoC love it for the right reasons, but its lack of convenient generics and list comprehensions makes the first week feel like filling in forms. Java and C# will do the job and nobody will be thrilled. C++ is for people who know exactly why they're picking it, and those people don't need my advice. Kotlin is pleasant, honestly the nicest compromise between speed and expressiveness, and if I were starting from a blank slate and wanted compiled-language performance without the pain, I'd look hard at it. JavaScript works fine until you try to do integer maths past 2^53, which AoC will, at some point, ask you to do, and then you discover BigInt and make a face. Ruby is a joy that people have forgotten about. And the esoteric stuff, the people solving it in spreadsheets or in awk or in a language they wrote themselves, I admire it the way I admire someone who crosses an ocean in a bathtub. I would not recommend the bathtub.
2023's day 12, the hot springs one, is a good test of all of this. It's a dynamic programming problem in disguise, with a part two that unfolds the input five times and kills brute force instantly. In Python the memoized solution is a few lines with `functools.cache` and a recursive function over indices. In Rust you build the same thing with a `HashMap` keyed on a tuple, and you quarrel with the borrow checker over where the cache lives, and you end up with something faster and about three times as long. I know people who prefer the second experience. I don't, on a weekday morning. On a Saturday, sure.
Day 24 that same year, the hailstones in 3D space, is the opposite case. Python has `z3-solver` if you're willing to pip install it, and a lot of people just threw the equations at it and collected the star. That felt like cheating to some of the purists, and I'm sympathetic, but honestly I think it's a better lesson than hand-deriving the linear algebra. Knowing when to hand a problem to a solver is a real skill. And day 25 was a minimum cut, where half the world imported `networkx` and the other half wrote Karger's algorithm and prayed. Python's ecosystem made one of those paths easy. Most other languages make you build it. That's an unfair advantage and it's real.
Strange thing, though. The advantage shrinks the longer you stay in the game. By the second or third December, what separates people is not the library, it's the instinct: seeing a puzzle and knowing, within a minute, that it's a BFS on an implicit graph, or that part two is a cycle-detection problem hiding under a billion iterations. 2022's day 11, the monkeys passing items around, is the purest example. Part two is a modular arithmetic trick. You multiply the divisibility moduli together and take everything modulo that product. That's it. No language will hand you that. You either have seen it or you haven't.
So what do I actually tell the person who asks?
If you've never done it and you want a good December, use Python. If you're fluent in something else and you're happy in it, use that, and ignore anyone who tells you your language is wrong, because they're usually selling a language. If you want a stretch, choose one, and tell yourself ahead of time that you are allowed to quit on day 15 without any shame at all. That's the only version of the plan I trust.
I'll add one more thing that I believe more than any of the above, though I can't prove it. The people who finish aren't the ones with the best tool. They're the ones who decided in advance what "success" meant. For some it's 50 stars. For some it's a clean repository. For me, lately, it's reading one other person's solution each day after I've finished mine, usually somebody doing it in a language I'd never touch, and noticing how differently they carved up the same problem. That's the closest I get to the reason I started.
I still haven't decided what I'm using this year. I've been staring at an empty folder for a week.
A social news and discussion community