I've taken part in Advent of Code for a number of years. For the past two, though, I've used it in a way its FAQ clearly advises against, and I've found it just as valuable.
Last year I used AI to see how efficiently I could solve the puzzles, and to test the limits of what AI could do. This year the AI usually has a puzzle solved within 30 minutes, so the solution isn't my goal any more. Learning is. Every puzzle gets solved in Go, Rust, Kotlin and Python, often in more than one way, and every day gets an in-depth explanation of the computer science underneath, with figures and animations. It's the first year I really feel I've learned how to use bitwise operations.
What the FAQ Says
The Advent of Code FAQ is direct. To "Should I use AI to solve Advent of Code puzzles?" it answers "No.", and then asks: "If you send a friend to the gym on your behalf, would you expect to get stronger?"
It's a fair question if the point is getting better at solving puzzles, but that hasn't been my point for two years. Last year's was measuring what AI could do, which is an exercise of its own. This year's is everything around the answer: how the same idea reads in four languages, why one solution beats another, and the ideas underneath, the ones I rarely use.
One Explanation, Four Languages
Each day starts with an explanation, written before any solution, that walks through the problem, the algorithm and the computer science behind it. Rust, Python, Go and Kotlin then solve the day from it. Where two approaches both teach something, Rust and Python take different ones, and Go and Kotlin each follow one of them, so the languages check each other.
The AI works through a day in two sittings. In the first, it reads Part 1, writes the explanation, sets the day up in all four languages, and proposes two or three approaches, each in plain terms and then technically. Then it stops. As with the forks in The Branches That Matter, the choice is the part I keep, and making it is how I learn what each approach is for. In the second sitting, the AI implements that choice in every language, takes Part 2 the same way, and animates the solution at work.
Nothing reaches adventofcode.com until all four languages agree. A dry-run script works out every language's answer without any way to submit it, and once they match, the Rust solution submits.
How the Languages Compare
Every run reports how long each part took. These are the first five days, each a single run of both parts, timed inside the program, with Rust in release mode:
day rust go python kotlin
01 Dusty Shed 0.11 ms 0.16 ms 1.2 ms 32 ms
02 Fulminating Farmland 0.03 ms 0.06 ms 0.66 ms 35 ms
03 Foggy Forest 5.8 ms 5.8 ms 169 ms 66 ms
04 Perilous Pier 1.4 ms 1.2 ms 13 ms 35 ms
05 Rhythmic Factory 1.4 ms 3.5 ms 18 ms 79 ms
Rust and Go are close: Rust is quicker on most days, they tie on Day 3, and Go edges ahead on Day 4. Day 4 is also the one day all four languages run the same algorithm in both parts, and there Python takes about ten times as long as Rust. Kotlin's times include the JVM warming up: each day runs once, in a JVM of its own, loading classes and compiling hot code as it goes. Python's 169 ms on Day 3 comes from a route of its own, which I'll come back to.
The more interesting differences are the ones that can change an answer, and the explanations' gotchas collect them:
- Remainders -
-1 % 6is 5 in Python, but −1 in Rust, Go and Kotlin. Day 2 counts round a cycle of six colours, so the Rust and Go solutions add 6 before taking the remainder, and the Kotlin one callsmodinstead of%. - Stable sorts - Rust's
sort_by_keykeeps equal items in the order they came in. Go'sslices.SortFuncdoesn't; the stable one isslices.SortStableFunc. Python'sCounter.most_common()breaks ties by first appearance, a third rule that can change Day 1's answer. - Integer width - a row of 141 positions fits in a single Python integer, but not in a Rust
u64, or even au128.
Day 1's animation shows two of those routes side by side. Rust counts in an array and ranks with a stable sort, while Python counts in a dictionary and keeps a heap of the three most common.
More Than One Way to Solve It
The routes don't just differ in style: sometimes one is far cheaper, and it isn't always the one that looks more efficient.
Day 4 asks for the shortest list of colours a printer can repeat to reproduce a damaged barcode. Wrap the barcode like text at some width, and each column makes up a class: the width works only if no class holds two different colours. Rust first took what looked like the more efficient route: compare each pair of positions once, and let every pair with different colours rule out the widths that divide the distance between them. The other languages check one width at a time, class by class, reading positions again for every width, but they stop at the first class that fails.
Part 2 joins everything into one barcode of 20,000 colours. The pair route has to compare 22.4 million pairs, while the class check rules out almost every width in its very first class, which holds only every p-th position. In all, it looks at fewer than 200,000 positions:
That's n times 1 + 1/2 + 1/3 + …, the harmonic series, which grows remarkably slowly: its first thousand terms add up to about 7.5. Rust's first route took about 14 milliseconds on Part 2; switched to the class check, it takes under a millisecond. The lazy route, which stops as soon as it knows, did over a hundred times less work than the eager one, which works everything out up front.
A better algorithm on paper didn't rescue the eager route either. The explanation shows how a fast Fourier transform can count the pairs with different colours at every distance without visiting them, in work that grows like n log n rather than n². Timed in Rust on my input, it takes 1.8 milliseconds: nearly eight times quicker than the pair route, but still more than twice as slow as the class check.
On a barcode with few readable colours, the class check would rarely stop early, and an eager route would be the better choice. On this one, the algorithm mattered more than the language.
Day 5 is a microwave whose buttons each change the number on its screen, and Part 1 wants every number that at most three presses can reach. Python and Kotlin try every sequence of up to three presses, 820 of them for nine buttons. Rust and Go go layer by layer, a breadth-first search that presses on each new number only once: just 92 different numbers come out.
The gap grows fast: up to eight presses make 48 million sequences, which end on just 345,033 different numbers. In Part 2 there's no limit on presses, so all four languages go layer by layer until a round finds nothing new. A four-digit screen shows only 10,000 numbers, so that round has to come: that's the pigeonhole principle.
Explanations That Teach
The explanations are where the learning happens, and they all have the same shape:
- Problem - the puzzle, retold in a few sentences.
- In plain terms - the solution in everyday words, before any jargon.
- Part 1 and Part 2 - the algorithm, worked through on a small example, with tables, figures and an animation of each part.
- Computer science ideas - each idea the day exercises, in four short paragraphs: the idea, how the puzzle uses it, where it turns up in everyday code, and a question to try before reading the answer.
- Gotchas - the traps in the rules, the input and the languages.
None of them quotes a puzzle. The FAQ asks anyone posting code not to include the puzzle text or their inputs, so every table and figure works on a small instance made up for it, with its numbers computed by a script rather than by hand. The approaches are checked against each other, and against brute force on random inputs.
The animations show the solution at work, with every route at once when the languages differ. They're plain SVG animations, so they play as an ordinary image, including on this page. Day 2's counts one distance two ways. Rust, Go and Kotlin add up a robot's hops round a cycle of six colours, while Python counts the laps it starts, because its position is always six times its laps plus the number of the colour it's on. That's an invariant, one of Day 2's three ideas.
The everyday-code paragraphs tie each idea back to work. Matching within a tolerance isn't transitive, so merging duplicate customers by similar names can chain Jon to John to Joan, which is Day 4's lesson about equivalence relations. git bisect is a binary search, so it only works if a bug, once in, stays in. Unix permissions are octal because each octal digit is exactly three bits.
Every idea ends with a question. Day 2's, on greedy algorithms: with coins worth 1, 3 and 4, how many coins does it take to pay 6 if you always take the largest coin that fits, and is that the fewest? Greedy pays 4 + 1 + 1, but 3 + 3 takes two. A greedy rule needs a proof that it works, and Day 2's Part 2 comes with one.
Five days in, the explanations have taught twenty ideas:
- Day 1 - Big-O notation, counting with an array, stable sorting and heaps.
- Day 2 - Modular arithmetic, invariants, and greedy algorithms with a proof that one is optimal.
- Day 3 - Canonical forms, bitsets, and rotating a vector a quarter turn.
- Day 4 - Periods of a string, equivalence relations, binary search on a monotone test, and the harmonic series.
- Day 5 - Positional notation, state spaces, breadth-first search, exponential growth, closures and fixed points, and the pigeonhole principle.
Finally Learning Bitwise
Bitwise operations came up on Days 3 and 4, and the explanations are why they've finally stuck.
Day 3 counts the squares that trees make on a map. Python stores each row as one integer, with bit c set when column c holds a tree. Two rows s apart hold a square of side s wherever both have trees in two columns that are also s apart:
both = row[r1] & row[r2] # columns where both rows have a tree
squares = both & (both >> s) # ... and so does the column s to the right
count += squares.bit_count() # one 1 bit per square
The first AND finds the columns both rows share. Shifting right by s moves every column s places toward column 0, so the second AND keeps the shared columns whose partner s to the right is shared too. Every bit left is a square, and each operation handles dozens of columns at once, not one at a time.
The gotchas cover what trips you up. Bit 0 is the last binary digit, so a row prints backwards: #.#.## is 0b110101, and the partner to the right comes back with >>, not <<. The trick suits Python because its integers grow as large as needed, so a row of 141 positions is a single number. In Rust, Go or Kotlin it would take three 64-bit words or a big-integer library, which is one reason those three search outward from each tree instead. It isn't the quickest route, as the 169 ms in the table shows, but it's the one that teaches bitsets.
Day 4 uses bits the other way round. Checking a class means asking whether it holds more than one colour. Python collects the colours in a set, while Rust and Kotlin gather them into the bits of a small integer and count those with a single call:
python all(len(set(barcode[k::p]) - {"?"}) <= 1 for k in range(p))
rust (0..p).all(|k| colours(barcode, k, p).count_ones() <= 1)
kotlin (0 until p).all { k -> colours(barcode, k, p).countOneBits() <= 1 }
Go does the same with bits.OnesCount32, in a plain loop: its standard library has neither all nor step_by.
Learning Is the Goal
With the answer arriving in 30 minutes, the stars still come, but they're no longer why I take part. I'm not on any leaderboard either, because I'm not trying to compete with anyone. The about page lists the ways people use the puzzles: interview prep, company training, university coursework, practice problems, a speed contest. This year mine is closest to coursework, with AI as the tutor.
I contribute to Advent of Code every year, and I hope it continues for a very long time. Its puzzles are designed to be interesting for humans to solve, and Eric Wastl keeps making them that way: even with the answer in half an hour, every one so far has had something to teach me.
Further Reading
- Advent of Code
- About Advent of Code, with the FAQ quoted above
- AoC++, to support the event
- The Branches That Matter