Add day01
This commit is contained in:
commit
e66d90983e
|
@ -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"
|
|
@ -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]
|
|
@ -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(())
|
||||
}
|
Loading…
Reference in New Issue