From fa26f694d8cf98272763952a698301ee9935c19e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Jun 2025 09:47:23 +0200 Subject: Added 2022, first few problems --- 2022/01/a.rs | 8 ++++++++ 2022/01/b.rs | 9 +++++++++ 2022/01/common.rs | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 2022/01/a.rs create mode 100644 2022/01/b.rs create mode 100644 2022/01/common.rs (limited to '2022/01') diff --git a/2022/01/a.rs b/2022/01/a.rs new file mode 100644 index 0000000..77e57e4 --- /dev/null +++ b/2022/01/a.rs @@ -0,0 +1,8 @@ +mod common; +use common::*; + +fn main() { + let e = get_elves_from_stdin(); + let m: i64 = e.iter().map(|a| a.iter().sum()).max().unwrap(); + println!("{m}"); +} diff --git a/2022/01/b.rs b/2022/01/b.rs new file mode 100644 index 0000000..ca0b324 --- /dev/null +++ b/2022/01/b.rs @@ -0,0 +1,9 @@ +mod common; +use common::*; + +fn main() { + let e = get_elves_from_stdin(); + let mut w = e.iter().map(|a| a.iter().sum()).collect::>(); + w.sort_by(|a, b| b.cmp(a)); + println!("{}", w[0] + w[1] + w[2]); +} diff --git a/2022/01/common.rs b/2022/01/common.rs new file mode 100644 index 0000000..8ed8b49 --- /dev/null +++ b/2022/01/common.rs @@ -0,0 +1,18 @@ +pub fn get_elves_from_stdin() -> Vec> { + let mut e = Vec::>::new(); + e.push(Vec::::new()); + let mut line = String::new(); + loop { + match std::io::stdin().read_line(&mut line).unwrap() { + 0 => break, + 1 => e.push(Vec::::new()), + _ => { + let n = line.trim().parse::().unwrap(); + let last = e.len() - 1; + e[last].push(n); + } + } + line.clear(); + } + e +} -- cgit v1.3