summaryrefslogtreecommitdiff
path: root/src/day_1.rs
blob: 6632baa545d629ba702a3808f0f1b5d5eae70453 (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pub fn run(input: Vec<String>)
{
    // Convert inputs to unsigned integer numbers
    let input: Vec<u32> = input
        .into_iter()
        .map(|s| {
            s.parse()
                .expect("All lines are required to be unsigned integers")
        })
        .collect();

    part1(&input);
    part2(&input);
}

fn part1(input: &[u32])
{
    let mut last_depth = u32::MAX;
    let num_down_slopes = input
        .iter()
        .filter(|&depth| {
            let going_deeper = *depth > last_depth;
            last_depth = *depth;

            going_deeper
        })
        .count();

    println!("Solution to part 1: {}", num_down_slopes);
}

fn part2(input: &[u32])
{
    let mut last_aliased_depth = u32::MAX;
    let num_aliased_down_slopes = input
        .windows(3)
        .filter(|&depths| {
            let aliased_depth: u32 = depths.iter().sum();
            let going_deeper = aliased_depth > last_aliased_depth;
            last_aliased_depth = aliased_depth;

            going_deeper
        })
        .count();

    println!("Solution to part 2: {}", num_aliased_down_slopes);
}