A little “warm-up” problem to begin the Advent of Code 2021.

Part 1

This part is straightforward (even if I took nearly half an hour to complete it, due to Rust module organization issues). We only have to figure out the number of list elements superior to their predecessors.

pub fn solve_part1(input: &Vec<u64>) -> u64 {
    let mut increases = 0;

    for i in 1..input.len() {
        if input[i] > input[i-1] {
            increases += 1;
        }
    }

    increases
}

Part 2

There is a little trick, that prevents useless computations. It is not necessary to compute slices sums (i.e sums of three consecutive list elements). In order to know if a slice has a greater sum than its predecessor, we only have to compare the last element of this slice to the first element of its predecessor.

pub fn solve_part2(input: &Vec<u64>) -> u64 {
    let mut increases = 0;

    for i in 3..input.len() {
        if input[i] > input[i-3] {
            increases += 1;
        }
    }

    increases
}

I managed to finish this part in only 6 minutes, and I gained 1000 places.