From eafd2387bbd1b8a78d77661e30a023e99830a8bd Mon Sep 17 00:00:00 2001 From: pjht Date: Thu, 1 Dec 2022 11:17:42 -0600 Subject: [PATCH] 2022 day 1 --- .gitignore | 3 +++ Cargo.toml | 10 ++++++++++ src/day01.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ 4 files changed, 53 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/day01.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1033ab7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/Cargo.lock +/input diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..04b351e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "aoc2022" +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/src/day01.rs b/src/day01.rs new file mode 100644 index 0000000..e7cd061 --- /dev/null +++ b/src/day01.rs @@ -0,0 +1,35 @@ +use aoc_runner_derive::{aoc, aoc_generator}; + +#[aoc_generator(day1)] +pub fn input_generator(input: &str) -> Vec> { + input + .lines() + .collect::>() + .split(|el| el == &"") + .map(|list| { + list.iter() + .map(|el| el.parse::().unwrap()) + .collect::>() + }) + .collect::>() +} + +#[aoc(day1, part1)] +pub fn solve_part1(input: &[Vec]) -> u64 { + let mut sums = input + .iter() + .map(|el| el.iter().sum::()) + .collect::>(); + sums.sort(); + sums.pop().unwrap() +} + +#[aoc(day1, part2)] +pub fn solve_part2(input: &[Vec]) -> u64 { + let mut sums = input + .iter() + .map(|el| el.iter().sum::()) + .collect::>(); + sums.sort(); + sums[sums.len() - 3..sums.len()].iter().sum::() +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5d1048b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +use aoc_runner_derive::aoc_lib; + +pub mod day01; + +aoc_lib! { year = 2022 }