From 3c70653791b15494352da983d1ecab4c90ad3879 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Thu, 1 Dec 2022 14:11:30 -0500 Subject: [PATCH] Also did #1 in rust. --- .gitignore | 3 ++- 2022/1/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 2022/1/main.rs diff --git a/.gitignore b/.gitignore index 5236e1e..14e091d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ - +*a.out +*main diff --git a/2022/1/main.rs b/2022/1/main.rs new file mode 100644 index 0000000..9a7e4b8 --- /dev/null +++ b/2022/1/main.rs @@ -0,0 +1,38 @@ +use std::fs; + +fn main() +{ + let contents = fs::read_to_string("data.txt") + .expect("Could not find data.txt."); + + let mut numbers = Vec::new(); + let mut total = 0; + let mut max = 0; + for line in contents.split('\n') + { + if line == "" + { + if total > max + { + max = total; + } + numbers.push(total); + total = 0; + } + else + { + let i = line.parse::().unwrap(); + total += i; + } + } + + println!("Max: {max}"); + + numbers.sort(); + let mut top_three = 0; + for amt in &numbers[numbers.len() - 3..numbers.len()] + { + top_three += amt; + } + println!("Top Three: {top_three}"); +}