From e271b0d585b02cd6416f0a0b422ef56a1f7f1434 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 1 Dec 2022 19:02:20 +0100 Subject: [PATCH] 2022 day1/rust: add solution --- 2022/day1/rust/Cargo.toml | 8 ++++++++ 2022/day1/rust/src/main.rs | 18 ++++++++++++++++++ Cargo.toml | 1 + 3 files changed, 27 insertions(+) create mode 100644 2022/day1/rust/Cargo.toml create mode 100644 2022/day1/rust/src/main.rs diff --git a/2022/day1/rust/Cargo.toml b/2022/day1/rust/Cargo.toml new file mode 100644 index 0000000..60bf433 --- /dev/null +++ b/2022/day1/rust/Cargo.toml @@ -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] diff --git a/2022/day1/rust/src/main.rs b/2022/day1/rust/src/main.rs new file mode 100644 index 0000000..29abb0c --- /dev/null +++ b/2022/day1/rust/src/main.rs @@ -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::().unwrap()) + .sum::() + }) + .collect(); + elves.sort_by_key(|&s| Reverse(s)); + + println!("{}", elves[0]); + println!("{}", &elves[..3].iter().sum::()); +} diff --git a/Cargo.toml b/Cargo.toml index f4d683f..02324d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ members = [ "2021/day14/rust", "2021/day16/rust", "2021/day22/rust", + "2022/day1/rust", ]