This commit is contained in:
pjht 2022-12-01 11:17:42 -06:00
commit 2685989a89
4 changed files with 53 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
/Cargo.lock
/input

10
Cargo.toml Normal file
View File

@ -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"

35
src/day01.rs Normal file
View File

@ -0,0 +1,35 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day1)]
pub fn input_generator(input: &str) -> Vec<Vec<u64>> {
input
.lines()
.collect::<Vec<_>>()
.split(|el| el == &"")
.map(|list| {
list.iter()
.map(|el| el.parse::<u64>().unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>()
}
#[aoc(day1, part1)]
pub fn solve_part1(input: &[Vec<u64>]) -> u64 {
let mut sums = input
.iter()
.map(|el| el.iter().sum::<u64>())
.collect::<Vec<_>>();
sums.sort();
sums.pop().unwrap()
}
#[aoc(day1, part2)]
pub fn solve_part2(input: &[Vec<u64>]) -> u64 {
let mut sums = input
.iter()
.map(|el| el.iter().sum::<u64>())
.collect::<Vec<_>>();
sums.sort();
sums[sums.len() - 3..sums.len()].iter().sum::<u64>()
}

5
src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
use aoc_runner_derive::aoc_lib;
pub mod day01;
aoc_lib! { year = 2022 }