summaryrefslogtreecommitdiff
path: root/src/bin/day1.rs
blob: eb4ee6a9b253e2a5f93a2a37519c93d3e2273723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() {
    let input = advent::read_numbers(1);
    println!("1a: {}", count_increasing(&input));
    println!("1b: {}", count_increasing_window(&input, 3));
}

fn count_increasing(numbers: &[usize]) -> usize {
    count_increasing_window(numbers, 1)
}

fn count_increasing_window(numbers: &[usize], window_size: usize) -> usize {
    numbers[window_size..].iter()
                .zip(numbers.iter())
                .map(|(&x,&y)| (x as isize) - (y as isize))
                .filter(|&x| x.is_positive())
                .count()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let numbers = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
        assert_eq!(count_increasing(&numbers), 7);
        assert_eq!(count_increasing_window(&numbers, 3), 5);
    }
}