Advent of code 2021: Day 1

This article is part of a series where I'll be diving head first into the Advent of code 2021. I'll be documenting the challenge of solving such a puzzle and how I got to the answer. I want to prefix this by stating that I can't cheat for any of these challenges; with that, I mean I can't look up any other implementations online.

In this article I'll be solving: Advent of code 2021: Day #1.

Part 1

I am given a file called input in which there is a list of 2000 numbers, which represent depth readings to a submarine sonar system. Part 1 of the puzzle asks how many times the depth increases. This is fairly simple in Rust:

fn depth_increases(depths: &Vec<u16>) -> usize {
    (0..depths.len() - 1)
        .filter(|&i| depths[i + 1] > depths[i])

Part 2

The second part is the same question, but asking which three-measurement sliding window is larger than the next; technically asking which consecutive sum of three measurements is larger. This can be solved like this:

fn sliding_depth_increases(depths: &Vec<u16>) -> usize {
    let group_size = 3;

    (0..depths.len() - group_size)
        .filter(|&i| {
            let sum: u16 = depths[i..i + 3].iter().sum();
            let next_sum: u16 = depths[i + 1..i + 4].iter().sum();

            next_sum > sum

Nothing too hard for day 1, and so I gained two stars ⭐️⭐️.

The full solution is available on GitHub.