2022 day1/rust: add solution
This commit is contained in:
parent
4be136b1a9
commit
e271b0d585
3 changed files with 27 additions and 0 deletions
8
2022/day1/rust/Cargo.toml
Normal file
8
2022/day1/rust/Cargo.toml
Normal 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]
|
18
2022/day1/rust/src/main.rs
Normal file
18
2022/day1/rust/src/main.rs
Normal 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>());
|
||||||
|
}
|
|
@ -9,4 +9,5 @@ members = [
|
||||||
"2021/day14/rust",
|
"2021/day14/rust",
|
||||||
"2021/day16/rust",
|
"2021/day16/rust",
|
||||||
"2021/day22/rust",
|
"2021/day22/rust",
|
||||||
|
"2022/day1/rust",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue