commit e66d90983ef44292fa6f524771a751d0e570b6e5 Author: MasterofJOKers Date: Thu Dec 15 16:58:38 2022 +0100 Add day01 diff --git a/day01/Cargo.lock b/day01/Cargo.lock new file mode 100644 index 0000000..683c0b9 --- /dev/null +++ b/day01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day01" +version = "0.1.0" diff --git a/day01/Cargo.toml b/day01/Cargo.toml new file mode 100644 index 0000000..5a61072 --- /dev/null +++ b/day01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day01" +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/day01/src/main.rs b/day01/src/main.rs new file mode 100644 index 0000000..230ea49 --- /dev/null +++ b/day01/src/main.rs @@ -0,0 +1,28 @@ +use std::error; +use std::fs::File; +use std::io::BufRead; +use std::io::BufReader; + + +fn main() -> Result<(), Box> { + let f = File::open("input.txt")?; + let reader = BufReader::new(f); + + let mut sums: Vec = Vec::new(); + let mut current_sum: u32 = 0; + for value in reader.lines() { + let value = value.unwrap(); + if value == "" { + sums.push(current_sum); + current_sum = 0; + } else { + current_sum += value.parse::()?; + } + } + sums.push(current_sum); + sums.sort(); + sums.reverse(); + + println!("{:?}", sums[..3].iter().sum::()); + Ok(()) +}