aboutsummaryrefslogtreecommitdiff
path: root/2022/01
diff options
context:
space:
mode:
Diffstat (limited to '2022/01')
-rw-r--r--2022/01/a.rs8
-rw-r--r--2022/01/b.rs9
-rw-r--r--2022/01/common.rs18
3 files changed, 35 insertions, 0 deletions
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 @@
1mod common;
2use common::*;
3
4fn main() {
5 let e = get_elves_from_stdin();
6 let m: i64 = e.iter().map(|a| a.iter().sum()).max().unwrap();
7 println!("{m}");
8}
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 @@
1mod common;
2use common::*;
3
4fn main() {
5 let e = get_elves_from_stdin();
6 let mut w = e.iter().map(|a| a.iter().sum()).collect::<Vec<i64>>();
7 w.sort_by(|a, b| b.cmp(a));
8 println!("{}", w[0] + w[1] + w[2]);
9}
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 @@
1pub fn get_elves_from_stdin() -> Vec<Vec<i64>> {
2 let mut e = Vec::<Vec::<i64>>::new();
3 e.push(Vec::<i64>::new());
4 let mut line = String::new();
5 loop {
6 match std::io::stdin().read_line(&mut line).unwrap() {
7 0 => break,
8 1 => e.push(Vec::<i64>::new()),
9 _ => {
10 let n = line.trim().parse::<i64>().unwrap();
11 let last = e.len() - 1;
12 e[last].push(n);
13 }
14 }
15 line.clear();
16 }
17 e
18}

Generated with cgit - Back to sebastiano.tronto.net