2022 day1/rust: add solution

This commit is contained in:
Xiretza 2022-12-01 19:02:20 +01:00
parent 4be136b1a9
commit e271b0d585
3 changed files with 27 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "rust_2022_01"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,18 @@
use std::{cmp::Reverse, io::stdin};
fn main() {
let lines: Vec<_> = stdin().lines().map(Result::unwrap).collect();
let mut elves: Vec<_> = lines
.split(|s| s.is_empty())
.map(|entries| {
entries
.iter()
.map(|c| c.parse::<u64>().unwrap())
.sum::<u64>()
})
.collect();
elves.sort_by_key(|&s| Reverse(s));
println!("{}", elves[0]);
println!("{}", &elves[..3].iter().sum::<u64>());
}

View file

@ -9,4 +9,5 @@ members = [
"2021/day14/rust",
"2021/day16/rust",
"2021/day22/rust",
"2022/day1/rust",
]