Skip to content

Instantly share code, notes, and snippets.

@matsadler
Last active December 5, 2022 00:38
Show Gist options
  • Save matsadler/ac8c4168f8775388827d938e88e6cb09 to your computer and use it in GitHub Desktop.
Save matsadler/ac8c4168f8775388827d938e88e6cb09 to your computer and use it in GitHub Desktop.
Advent of Code 2022 in Rust, stdlib only.
use std::{env, error::Error, fs};
fn main() -> Result<(), Box<dyn Error>> {
let mut args = env::args();
let _name = args.next().ok_or("no program name")?;
let path = args.next().ok_or("missing path")?;
let input = fs::read_to_string(path)?;
let result = input
.split_terminator("\n\n")
.map(|s| {
s.split_terminator('\n')
.map(|s| s.parse::<i64>())
.sum::<Result<i64, _>>()
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.max()
.ok_or("couldn't determine max")?;
println!("{result}");
Ok(())
}
Advent of Code 2022 in Rust, stdlib only.
As a single file is used per day, with no external libraries `rustc` is used
without `cargo`. A helper script `cult` wraps `rustc` invocations in `cargo`ish
commands.
For example, to build and run day 1, run:
./cult 01 path/to/input.txt
#!/bin/sh
# a terrible build tool that does none of the things cargo does
command="$1"
shift
name="$1"
shift
case "$command" in
build)
rustc --edition 2021 "$name".rs
;;
run)
rustc --edition 2021 "$name".rs && ./"$name" $@
;;
fmt)
rustfmt --edition 2021 "$name".rs
;;
clippy)
clippy-driver --edition 2021 "$name".rs
;;
clean)
rm "$name"
;;
*)
echo "unknown command"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment