Add day01

This commit is contained in:
MasterofJOKers 2022-12-15 16:58:38 +01:00
commit e66d90983e
3 changed files with 43 additions and 0 deletions

7
day01/Cargo.lock generated Normal file
View File

@ -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"

8
day01/Cargo.toml Normal file
View File

@ -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]

28
day01/src/main.rs Normal file
View File

@ -0,0 +1,28 @@
use std::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut sums: Vec<u32> = 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::<u32>()?;
}
}
sums.push(current_sum);
sums.sort();
sums.reverse();
println!("{:?}", sums[..3].iter().sum::<u32>());
Ok(())
}