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}"); +}