Advent of rust

Tagged with: rust adventofcode

Advent of code has been on my radar for a while but I've never quite mustered the enthusiasm to compete. Similarly, I've been meaning to learn rust for what feels like forever but could never quite find the time. So this year I combined the two to help me get over the hump. On the way I learned a few nice tricks I think worth sharing, even if only with future me.

Getting set up

Learning a new language always comes with some setup headaches and this was no different, but the solutions I found are things I'll likely carry over to other personal projects.

Direnv

How did I not know about direnv! Direnv isn't tied to Nix but, since I'm using NixOS, setting it up is as simple as adding programs.direnv.enable = true; in configuration.nix and then making an appropriate adjustment for your shell. I added the appropriate emacs config too, and then for each project for which I want local setup I run echo "use nix" > .envrc && direnv allow and write a shell.nix like so:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    pkgs.cargo
    pkgs.openssl
    pkgs.pkg-config
    pkgs.rust-analyzer
    pkgs.rustc
    pkgs.rustfmt
  ];

  shellHook = ''
    export RUST_LOG=debug
    export RUSTUP_HOME=$HOME/.rustup
    export CARGO_HOME=$HOME/.cargo
    export PATH=$CARGO_HOME/bin:$PATH
  '';
}

For my advent of code this meant I had my rust toolchain installed, and that it could compile things like the reqwest package (which needs access to openSSL).

Docs vs artificial intelligence (AI)

AIs are very zeitgeisty just now. For each feature I wanted to learn in rust—and remember I was starting from scratch there so this could be very basic stuff like folding—I tried asking chat GPT what the best way to do it was. I then checked answers by, for example, reading a relevant section of the rust book or another published work. For now, the docs are still a much better learning experience for me. I frequently found AI's explanations unsatisfying, and if I asked it to write code directly I didn't ever feel like I was really learning anything. I won't be integrating an AI into my emacs setup just yet.

The competition itself

I knew from the start I wasn't going to be making much of a dent on the leaderboards given I couldn't even iterate over a list in rust before late November. But I was pretty happy with initial progress, in that I completed all the puzzles over the first few days without much trouble. When the difficulty ramped up, things got particularly interesting.

Algorithms

I hadn't encountered the n queens problem until day 4 of this contest. My background was not originally in computer science so I've tended to pick up new skills as I needed them, and things like backtracking algorithms haven't really come up in my career thus far.

This is really valuable exposure and encourages one to think about problems very differently than how we might under ordinary circumstances. In particular, once I saw this come up I decided to start each puzzle by describing the problem in as abstract a way as I could to see if there was an established algorithm for solving it, which I could then learn along with any Rust concepts I was looking to pick up that day.

Aside: day five led me to working with directed acyclic graph traversal. This should've been very familiar to me but was actually quite hard and was the first problem I had to carry over to the next day.