Advent of Code 2024
February 4, 2024
Table of Contents
Which programming language
I’ve been doing Advent of Code (AoC) in Lua for a few years now but every year, around the halfway mark, I start getting annoyed by having to write the same boilerplate code over and over again. Like many languages, table (~ object) equality compares elements by reference, not by content. As a result, using tables as keys in other tables may not work the way you’d want it to. This is especially annoying in AoC, where you rely on sets and maps a lot. I’d love to take a list of points expressed as p = { 1, 4 }
and make them unique by using a set-as-a-table. But putting { 1, 4 }
in a table twice, will just create two separate entries. For AoC 2024 I either want to shop around for a new language, one that has good support for hash maps and sets, or find a small enough snippet of boilerplate code that I can add to a template file, so that I can keep using Lua.
If I could conjure up a language out of thin air, specifically for AoC, what would it look like?
- one way of doing things; I love that in Go there’s basically one way to iterate through things and that’s
range
- content equality or whatever it’s called when
{} == {} // true
- single file scripts; I really don’t like it when I need to initialize some sort of project with lots of files just to write a one liner for AoC
- pattern matching particularly on lists; it’s really cool if you can parse stuff by splitting on whitespace and then pattern matching on the fragments
- types that get out of my way; for longer AoC days having types helps with refactoring but I don’t want to have 50% of the code just be type “noise” for small programs where I can easily keep everything in my head
- basic container types in the stdlib (looking at you Haskell)
The most promising language so far is actually Rust! I can use tuples for points and those I can insert into sets and maps. It unfortunately doesn’t make single file scripts easy. I think you have to use some Nix magic to make it work. It also does not have “one way of doing things”, quite the opposite really. Over the past two weeks I’ve been going through AoC 2017 in Rust with a friend and we probably spend as much time talking about the neatest, most concise method chains, as we spend talking about the problem itself. I think Bun and Deno and all the Javascript variations inherit its equality model and are thus out of the question. Lisps are probably another group of languages I should look at. I did a few days in Janet for 2023 and I think I also have some Clojure code lying around somewhere. Come to think of it, I’m not sure why I gave up on the languages. For Clojure it might just have been the Java behemoth lurking under the surface. But Janet was a lot of fun. I should look into that again, specifically with regards to equality.
TL;DR: I want a tiny language where I can do {} == {} // true
. Rust isn’t tiny but its tuples are like that. I want to revisit Janet as well.
My rules for 2024
- No imports, not even local modules. If you want to be competitive, you don’t want to reimplement things all the time. But if you’re looking for fun (I am!) then sticking to whatever your language’s stdlib gives you is a great way to learn more about algorithms and to get to know your language better.
- One file per day, must solve both parts. Because why not.
- Optimize for computational complexity, then readability. I’d rather have ugly code that traverses a list once, then beautiful code that does it twice (at work it’s the opposite).
- Only visit Reddit when I’ve solved both parts.
- No help other than the stdlib documentation.
- Maximum of 2h per day.
About those last two rules. AoC is both a source of fun but also of significant stress. As the difficulty of the puzzles ramps up, it’s getting harder and harder to finish things between 8am and 10am (before work). Eventually things get so difficult that I’ll waste spend at least one full day on one or two puzzles. But at the same time I feel pressured to submit a solution in the Reddit megathread on the same day. I don’t know why I feel like that, but it’s hard to ignore it. Over the past two weeks I’ve been going through AoC 2017 with a friend, to learn Rust. And since we have all the time in the world, we figure out all solution entirely on our own. And it’s so much fun! I’ve never had more fun with AoC than right now. That prompted me to reconsider how I want to do AoC 2024. And I decided that I’ll try to get as far as I can in the morning, on my own, with just the standard library and a blank sheet of virtual paper. If I finish a day during that time frame I can still post on Reddit and compare my solution to others, but I no longer want December to be this crazy month where my sleep schedule falls apart, my nutrition and workout routine are in shambles, and I’m mentally absent from most human interactions, because my brain is busy trying to make sense of tiny boxes in space.