diff --git a/2022/Cargo.lock b/2022/Cargo.lock index 130e8e6..70a346b 100644 --- a/2022/Cargo.lock +++ b/2022/Cargo.lock @@ -3,7 +3,7 @@ version = 3 [[package]] -name = "day01" +name = "aoc2022" version = "0.1.0" dependencies = [ "sorted-vec", diff --git a/2022/Cargo.toml b/2022/Cargo.toml index 82c3c74..1f06ed5 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "2022" +name = "aoc2022" version = "0.1.0" edition = "2021" diff --git a/2022/src/day01.rs b/2022/src/day01.rs index 6f60736..d229563 100644 --- a/2022/src/day01.rs +++ b/2022/src/day01.rs @@ -1,4 +1,3 @@ -use std::env; use std::io; use std::io::prelude::*; use std::fs::File; @@ -9,14 +8,7 @@ fn read_lines(filename: &str) -> io::Result>> { Ok(io::BufReader::new(file).lines()) } -fn get_step() -> i32 { - for arg in env::args() { - if arg == "2" { return 2 } - } - return 1 -} - -fn update_max(max: &mut SortedVec, current: i32, step: i32) { +fn update_max(max: &mut SortedVec, current: i32) { if max.is_empty() { max.insert(current); return @@ -24,15 +16,10 @@ fn update_max(max: &mut SortedVec, current: i32, step: i32) { if current <= max[0] { return } max.insert(current); - match step { - 2 => { if max.len() > 3 { max.remove_index(0); } }, - _ => { max.remove_index(0); }, - } + if max.len() > 3 { max.remove_index(0); } } pub fn execute() -> Result<(), io::Error> { - let step = get_step(); - let mut max: SortedVec = SortedVec::new(); let mut current = 0; @@ -41,12 +28,15 @@ pub fn execute() -> Result<(), io::Error> { current = match text.parse::() { Ok(value) => current + value, Err(_) => { - update_max(&mut max, current, step); + update_max(&mut max, current); 0 }, } } - println!("{}", max.iter().fold(0i32, |sum, i| sum + (*i as i32))); + if let Some(x) = max.iter().last() { + println!("Step 1 solution: {x}"); + } + println!("Step 2 solution: {}", max.iter().fold(0i32, |sum, i| sum + (*i as i32))); Ok(()) }