This repository contains my solutions for "Advent of Code 2024".
-
Bazelisk - used to manage Bazel, which in turn is used to manage all compilers, dependencies, libraries and binaries, and their tasks.
-
Rust - The Rust programming language (the toolchain itself is managed by Bazel, but you will likely need a local copy in order to use with your IDE/editor)
-
Run a single day:
bazel run --config=release //day_<day number> \-- <args>
-
Run tests for a single day:
bazel test //day_<day number>:all
-
If you update any of the dependencies, you will need to regenerate the crate index. To do that, run the following command:
CARGO_BAZEL_REPIN=1 bazel sync --only=crates
To add a new day to the repository, follow this list of instructions:
-
Run
cargo new day_<day number> --bin
. This should produce a new folder for the source code of that day, as well as add it to the rootCargo.toml
. -
Add an entry for the new folder to the
MODULE.bazel
file in thecrates.from_cargo
manifests. This will allow Bazel to graph and build the dependencies for the binary, as well as the binary itself. -
Add any basic dependencies to the day's
Cargo.toml
. Most days use the sharedaocutils
library, as well as dependencies likeclap
anditertools
.
[dependencies]
aocutils = { path = "../aocutils" }
clap = { version = "4.5.22", features = ["derive", "cargo"] }
itertools = { version = "0.13.0" }
- Create a
BUILD
file for the new day. You can use the other day's build files as a template, but it should generally look something like this:
load("@crates//:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_binary")
rust_binary(
name = "day_<day number>",
srcs = ["src/main.rs"],
data = glob(["inputs/*.txt"]),
deps = [
"//aocutils",
] + all_crate_deps(normal = True),
)
- Bootstrap the basic day solving program - most days start off the same, so starting off with the common main is a good idea.
use clap::Parser;
use std::{env, path};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long, default_value = "example.txt")]
input: String,
}
fn main() {
let args = Args::parse();
let crate_name = clap::crate_name!();
let cli_inputpath = path::Path::new(&args.input);
println!("Hello to solver for {crate_name}");
// TODO: (Stefan) Solve today's problem! ⭐⭐
}
-
Create a subfolder in the day called
inputs
withexample.txt
as a file. This file can then be filled with the example input given by Advent of Code. -
Resync all dependencies - only needs to be done once, since we are adding a new target.
CARGO_BAZEL_REPIN=1 bazel sync --only=crates
- Build and run the new day
bazel run --config=debug //day_<day number> -- --input="example.txt"