use super::PuzzleSolver;
#[derive(Debug)]
pub struct Solver {
pub input: Vec<u32>,
}
impl PuzzleSolver for Solver {
fn part_one(&self) -> String {
println!("counting the number of times a depth measurement increases");
count_increases(&self.input).to_string()
}
fn part_two(&self) -> String {
println!("counting the number of times the sum of 3 consecutive measurements increases");
count_range_increases(&self.input, 3).to_string()
}
}
impl Solver {
pub fn new(input: &str) -> Solver {
let integers: Vec<u32>;
integers = input
.trim()
.split('\n')
.into_iter()
.map(|x| x.parse().expect("Not a number!"))
.collect();
Solver { input: integers }
}
}
fn count_increases(input: &[u32]) -> u32 {
let mut counter = 0;
for pair in input.windows(2) {
if pair[0] < pair[1] {
counter += 1
}
}
counter
}
fn count_range_increases(input: &[u32], window_size: usize) -> u32 {
let sums: Vec<u32> = input
.windows(window_size)
.map(|depths| depths.iter().sum())
.collect();
count_increases(&sums)
}