diff --git a/2022-aoc/.gitignore b/2022-aoc/.gitignore new file mode 100644 index 0000000..3ceb93d --- /dev/null +++ b/2022-aoc/.gitignore @@ -0,0 +1,2 @@ +/target +/input diff --git a/2022-aoc/Cargo.toml b/2022-aoc/Cargo.toml new file mode 100644 index 0000000..03aa5e0 --- /dev/null +++ b/2022-aoc/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "aoc" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +aoc-runner = "0.3.0" +aoc-runner-derive = "0.3.0" diff --git a/2022-aoc/src/d1.rs b/2022-aoc/src/d1.rs new file mode 100644 index 0000000..ce14b48 --- /dev/null +++ b/2022-aoc/src/d1.rs @@ -0,0 +1,40 @@ +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; + +#[aoc_generator(day1)] +fn parse_input_day1(input: &str) -> Vec { + let mut cur = 0; + let mut out = Vec::new(); + for line in input.lines() { + if let Ok(n) = line.parse::() { + cur += n; + } else { + out.push(cur); + cur = 0; + } + } + + out +} + +#[aoc_run(day1, part1)] +fn part1(calories: &[i32]) -> i32 { + unsafe { *calories.iter().max().unwrap_unchecked() } +} + +#[aoc_run(day1, part2)] +fn p2(cals: &[i32]) -> i32 { + let mut cals = std::collections::BinaryHeap::from_iter(cals.iter().copied()); + let mut out = 0; + for _ in 0..3 { + out += cals.pop().unwrap(); + } + out +} + +// much slower than using a heap +#[aoc_run(day1, part2, alt)] +fn p2a(cals: &[i32]) -> i32 { + let mut cals = cals.to_owned(); + cals.sort_unstable_by(|x, y| y.cmp(x)); + cals.iter().take(3).sum() +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs new file mode 100644 index 0000000..03fef30 --- /dev/null +++ b/2022-aoc/src/lib.rs @@ -0,0 +1,5 @@ +use aoc_runner_derive::aoc_lib; + +mod d1; + +aoc_lib! { year = 2022 } diff --git a/2022-aoc/src/main.rs b/2022-aoc/src/main.rs new file mode 100644 index 0000000..0d8070a --- /dev/null +++ b/2022-aoc/src/main.rs @@ -0,0 +1,3 @@ +use aoc_runner_derive::aoc_main; + +aoc_main! { lib = aoc }