My ๐ Advent of Code solutions. Includes a runner and benchmarker with free Christmas trees ๐.
The following commands use a Cargo alias cargo advent
defined in
.cargo/config.toml
to run the solutions. This tool will automatically fetch
the input for the puzzle and cache it locally when the solution is run for the
first time. This requires the ADVENT_SESSION
environment variable to be set.
You can find this under the cookie name "session" in your logged in Advent of
Code browser session. This tool follows the automation guidelines on the
/r/adventofcode wiki.
export ADVENT_SESSION="533..."
Solutions can be run using the run
command. Just pass in the year and day. For
example, the following will run the solution for 2020 day 18.
cargo advent -y 2020 -d 18 run
Tests can be run using the test
subcommand.
cargo advent -y 2020 -d 18 test
Benchmarks can be run using the bench
subcommand.
cargo advent -y 2020 -d 18 bench
Extra arguments can be passed to both Cargo and the binary. Arguments after the
first --
argument will be passed to Cargo and arguments after the second --
will be passed to the actual binary. For example if we wanted the JSON output of
the benchmark we could run the following.
cargo advent -y 2020 -d 18 bench -- --features=json -- --output json
All of the above will be built using --release
.
Use the following to add a template for a new solution.
cargo advent -y 2022 -d 1 new
Open the browser for the given problem
cargo advent -y 2020 -d 7 open
You can use the provided runner and benchmarker for your own solutions. To get started simply add the crate to the Cargo manifest for your solution.
[dependencies]
advent = { git = "https://github.com/rossmacarthur/advent" }
Then use the following as your main function.
fn main() {
let solution = advent::new(parse_input).part(part1).part(part2).build();
solution.cli()
}
Where
parse_input
is a function that returns any typeI
implementingClone
.- Each part function takes
I
as an argument and returns something implementingDisplay
.
Finally, cli()
will instantiate a command line interface and run the program.
Ordinary runs will run each part once and output the answers. Passing --bench
to the program will perform a benchmark.
That's all! You're free to structure your program however else you want. See template.rs for the template I use or any of the solutions in this crate for an example.
Run and benchmark output:
There are also some optional features which pull in some other crates.
festive
enables some festive ascii art and changes the default output to--output festive
json
supports JSON output using--output json
, useful for collecting benchmark informationprelude
re-exports my prelude crate that can be imported usinguse advent::prelude::*;
They can be enabled in your Cargo manifest like this:
[dependencies]
advent = { git = "https://github.com/rossmacarthur/advent", features = ["festive", "json"] }
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.