Implement step 2 for day 1.
This commit is contained in:
parent
577da4ab52
commit
e04df4b548
9
2022/day01/Cargo.lock
generated
9
2022/day01/Cargo.lock
generated
|
@ -5,3 +5,12 @@ version = 3
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "day01"
|
name = "day01"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"sorted-vec",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sorted-vec"
|
||||||
|
version = "0.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c6734caf0b6f51addd5eeacca12fb39b2c6c14e8d4f3ac42f3a78955c0467458"
|
||||||
|
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
sorted-vec = "0.8.3"
|
||||||
|
|
|
@ -1,14 +1,39 @@
|
||||||
|
use std::env;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use sorted_vec::SortedVec;
|
||||||
|
|
||||||
fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
|
fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
|
||||||
let file = File::open(filename)?;
|
let file = File::open(filename)?;
|
||||||
Ok(io::BufReader::new(file).lines())
|
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<i32>, current: i32, step: i32) {
|
||||||
|
if max.is_empty() {
|
||||||
|
max.insert(current);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if current <= max[0] { return }
|
||||||
|
|
||||||
|
max.insert(current);
|
||||||
|
match step {
|
||||||
|
2 => { if max.len() > 3 { max.remove_index(0); } },
|
||||||
|
_ => { max.remove_index(0); },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), io::Error> {
|
fn main() -> Result<(), io::Error> {
|
||||||
let mut max = 0;
|
let step = get_step();
|
||||||
|
|
||||||
|
let mut max: SortedVec<i32> = SortedVec::new();
|
||||||
let mut current = 0;
|
let mut current = 0;
|
||||||
|
|
||||||
for line in read_lines("input.txt")? {
|
for line in read_lines("input.txt")? {
|
||||||
|
@ -16,12 +41,12 @@ fn main() -> Result<(), io::Error> {
|
||||||
current = match text.parse::<i32>() {
|
current = match text.parse::<i32>() {
|
||||||
Ok(value) => current + value,
|
Ok(value) => current + value,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
if current > max { max = current; }
|
update_max(&mut max, current, step);
|
||||||
0
|
0
|
||||||
},
|
},
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{}", max);
|
println!("{}", max.iter().fold(0i32, |sum, i| sum + (*i as i32)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user