Advent of Code 2022: Day 1

Published December 1, 2022 on Chandler Swift's Blog Source


This is it: I’m finally going to finish Advent of Code this year! (A strong claim, given that this is my third attempt, but we’ll see!) This year, I’m planning to use this as an excuse to learn/relearn Emacs and Rust.

I also started AoC in 2020 (19 stars; Python, mostly) and 2021 (36 stars; Rust). Last year, I went in with the specific intention of learning Rust, which honestly went pretty well; the challenges got more difficult at a pretty similar rate to which the language grew more familiar, so I ended up spending a pretty consistent amount of time on each day’s challenge.

This year, I’m going to continue with Rust, but with a different learning component: Emacs! I’ve been using Vim and VS Code as my primary editors for quite a while now, but Emacs came back across my radar yesterday as I saw the news of the looming Emacs 29 release. Lots of neat new features (Eglot for LSP integration, and native Wayland support) make this a pretty exciting release.

I used Emacs a bit in school (it was the required text editor for our second-semester CS class), so I’m not going in totally blind, but I’ve…basically forgotten everything I ever knew. C-X C-S to save; C-X C-C to exit? Arrow keys for navigation, type to insert text. That’s literally it, off the top of my head. That should get me through today, anyway; maybe tomorrow I’ll do the tutorial.

VS Code makes Rust easy; built-in syntax highlighting and good completion with the rust-analyzer extension really minimize the amount of thinking I have to do. With Emacs, I won’t be able to take the same tab-completion mental shortcuts. That means I’ll be relying a lot more heavily on my knowledge of the language to carry me through, as well as exploring some new editor features, which I’m excited about.

Setting up a dev environment

I already had Rust installed from last year’s attempt, so that’s easy: rustup update stable and I have the latest version ready to go. (I installed rustup through my system’s package manager, rather than the curl | bash official install.)

Emacs 29 isn’t stable, so the Arch package is currently still on v28. The various versions in the AUR all appear to be tracking the master branch, so I’m just going to go ahead and compile it myself. Apparently I had all the dependencies installed, so that was as simple as:

git clone git://git.savannah.gnu.org/emacs.git
cd emacs
./configure --with-pgtk
make -j16
./src/emacs

Some notes: https://lars.ingebrigtsen.no/2014/11/13/welcome-new-emacs-developers/

The ./configure step could be left out, but then Emacs fails looking for an X display. (I’m not entirely sure why it didn’t use Xwayland, which I do have set up.)

Code

use std::fs;

fn main() {
    let data = fs::read_to_string("input.txt")
        .expect("Could not read input.txt");
    let data = data.trim();

    let mut highest_totals = vec![0, 0, 0];
    let mut current_total = 0;
    for line in data.split("\n") {
        if line == "" {
            // we've reached the end of an elf's food
            highest_totals.push(current_total);
            highest_totals.sort_by(|a, b| b.cmp(a));
            highest_totals.truncate(3);
            current_total = 0;
        } else {
            current_total += line.parse::<u32>().unwrap();
        }
    }
    highest_totals.push(current_total);
    highest_totals.sort_by(|a, b| b.cmp(a));
    highest_totals.truncate(3);
    println!("{:?}, sum: {}", highest_totals, highest_totals.iter().sum::<u32>());
}

Notes

I’m using Emacs in GUI mode. I’ll probably also explore the -nw option to run it from a terminal.

Emacs didn’t have any syntax highlighting for Rust in its default configuration. I hear it has tree-sitter support as of v29, but I assume I need an extension to make it support any particular language? Not sure; a project for tomorrow!

Indentation by default is pretty wonky; Emacs appears to default to a tab inserting…three spaces? But then it replaces eight spaces with a tab? But eight isn’t a multiple of three? I fixed some indentation manually; tomorrow I’ll have to look a bit more into what’s going on here. For today, I just fixed things up manually with rustfmt.

I’m not entirely sure I understand what the pgtk stuff in Emacs means. I’ll have to check that out. More info here? https://batsov.com/articles/2021/12/19/building-emacs-from-source-with-pgtk/

Here’s where I wound up. Note that I don’t have the faintest idea how to kill the default intro buffer at the moment, and that I’m missing any kind of window decorations (though sway adds its own for me; not shown here).

Emacs displaying the final source code

Time spent

Download/Compile Emacs: 20m

Write/compile/submit code: 20m (~roughly evenly split between fighting Emacs and Rust)

Blog post: 1h

Soundtrack

Earfood, by the Roy Hargrove Quintet


I don't have a formal commenting system set up. If you have questions or comments about anything I've written, send me an email and I'd be delighted to hear what you have to say!