diff --git a/2022/01/rust/main.rs b/2022/01/rust/main.rs index 639edd1..c2750d0 100644 --- a/2022/01/rust/main.rs +++ b/2022/01/rust/main.rs @@ -1,5 +1,24 @@ -fn main() { - // Part A +#![feature(test)] + +extern crate test; + +#[cfg(test)] +mod tests { + use super::*; + use test::Bencher; + + #[bench] + fn bench_a(b: &mut Bencher) { + b.iter(|| part_a()); + } + + #[bench] + fn bench_b(b: &mut Bencher) { + b.iter(|| part_b()); + } +} + +fn part_a() { let result: u32 = include_str!("../input.txt") .trim() .split("\n\n") @@ -8,3 +27,19 @@ fn main() { .unwrap(); println!("A: {}", result); } + +fn part_b() { + let mut result: Vec = include_str!("../input.txt") + .trim() + .split("\n\n") + .map(|x| x.lines().map(|x| x.parse::().unwrap()).sum::()) + .collect(); + result.sort(); + let total = &result.as_slice()[result.len() - 3..result.len()]; + println!("B: {:?}", total.iter().sum::()); +} + +fn main() { + part_a(); + part_b(); +} diff --git a/Cargo.toml b/Cargo.toml index 08fdd37..430580e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,5 @@ edition = "2021" [[bin]] name = "2022-01" path = "./2022/01/rust/main.rs" + +# INJECT HERE diff --git a/README.md b/README.md index eb9a694..ef604ef 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,37 @@ # Advent Of Code -> New days are generated with [`hygen`](https://github.com/jondot/hygen). -> -> `bun run gen --year 2022 --day 1` +## Getting started + +New "days" are generated with [`hygen`](https://github.com/jondot/hygen). This will bootstrap code for python and rust. See details about the specifica languages below + +```bash +# First time setup +bun i + +# Generate +bun run gen --year 2022 --day 1 +``` + +## Python + +## Rust + +Run a single day: + +```bash +cargo run --bin 2022-01 +``` + +Bench a day: + +```bash +cargo bench --bin 2022-01 +``` + +Bench all days: + +```bash +cargo bench +``` + +> The benchmark is the built in Rust one. This is the [reason](https://doc.rust-lang.org/cargo/commands/cargo-bench.html) for the _nightly_ version. diff --git a/_templates/riddle/new/rust.ejs.t b/_templates/riddle/new/rust.ejs.t index f2e0e10..a13f82d 100644 --- a/_templates/riddle/new/rust.ejs.t +++ b/_templates/riddle/new/rust.ejs.t @@ -2,8 +2,37 @@ to: <%= dir %>/rust/main.rs unless_exists: true --- -fn main() { - // Part A - let contents = include_str!("../test.txt").trim(); - println!("A: {}", contents); +#![feature(test)] + +extern crate test; + +#[cfg(test)] +mod tests { + use super::*; + use test::Bencher; + + #[bench] + fn bench_a(b: &mut Bencher) { + b.iter(|| part_a()); + } + + #[bench] + fn bench_b(b: &mut Bencher) { + b.iter(|| part_b()); + } +} + +fn part_a() { + let result = include_str!("../input.txt"); + println!("A: {}", result); +} + +fn part_b() { + let result = include_str!("../input.txt"); + println!("A: {}", result); +} + +fn main() { + part_a(); + part_b(); } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly"