---
title: 'Advent of Code 2021 Day 1'
url: 'https://blog.philippludewig.de/de/writing/advent-of-code-2021-day-1'
markdown: 'https://blog.philippludewig.de/de/writing/advent-of-code-2021-day-1.md'
lang: de
date: '2021-12-02'
description: "Aloha people, it's the time of the year again where coding elves earn some internet points and learn cool stuff while doing that. Ofc I am talking about advent of code. This year I want to use a recently learned language and re-visit some I haven't used in a while. I also try to pair with some col…"
taxonomy:
  category:
    - blog
    - '2021'
    - Coding
  tag:
    - 'advent of code'
---

# Advent of Code 2021 Day 1

Aloha people,

it's the time of the year again where coding elves earn some internet points and learn cool stuff while doing that. Ofc I am talking about [advent of code](https://adventofcode.com/). This year I want to use a recently learned language and re-visit some I haven't used in a while. I also try to pair with some colleagues on the puzzles as I am still on the beach and this are the perfect coding exercises between the cloud provider workshops.

In these years advent of code the story starts with a careless elf who has dropped the sleigh keys into the ocean. Ohhh no! Good that the elves have a submarine ready to go!

![](https://i.giphy.com/media/8vIFoKU8s4m4CBqCao/giphy.webp)

[Day one](https://adventofcode.com/2021/day/1) informs us that the sonar sweep has returned some results (our puzzle input) and now we need to find the count of increases in these measurements. I have chosen to use [Rust](https://www.rust-lang.org/) for this one. The code can be found [here](https://github.com/DiesDasJenes/advent_of_code/tree/master/2021/adc_d1). My approach to the first part of the puzzle was rather simple:

- Read in the file as a vector of u32
- Iterate over the measurements and increase a counter when ever there is an increase
- Profit

Interestingly is that it was my first time using IntelliJ to create the Rust project, and it took me no time to get first results. I just wonder whether all unit tests are to be put into the production class itself, for now I am following what I see in the Rust workshops I have visited. Here is the method for part one of the puzzle. We expect a reference from a vector which contain unsigned numbers. I am still a beginner with this language so the decision whether to use i32 or u32 I made simply because all values in the puzzle input were positive.

```Rust
fn count_increases_of_measurements(measurements: &Vec<u32>) -> u32 {
    let mut count_of_increases: u32 = 0;
    let mut latest_increased_measurement: &u32 =
        measurements.first().unwrap();

    for measurement in measurements {
        if measurement > latest_increased_measurement {
            count_of_increases = count_of_increases.add(1)
        }
        latest_increased_measurement = measurement;
    }

    return count_of_increases;
}
```

Now that solved part one of the puzzle only so that part two could give me a headache, but Rust came to the rescue. The new puzzle is to implement a sliding window over the vector so that we can calculate the sum of it's tuple. Let me visualise this:

```text
199  A      
200  A B    
208  A B C  
210    B C D
200  E   C D
207  E F   D
240  E F G  
269    F G H
260      G H
263        H
```

The puzzle is to calculate the sum of `199,200,208` and then switching to the next tuple `200,208,210` and so on. I never have seen this kind of problem, and so I was wondering for a short time how to solve this. In the end it's not really hard. You would create slices from the vector and save the sum of them to another vector. I was fiddling around with the vector as Rust showed me this great method `windows(n)`. It creates a list of tuples which I could just add up to get the sum. Voilà and so I had solved part two of day one.

```Rust
fn collect_sum_of_triple_measurements(measurements: &Vec<u32>) -> Vec<u32> {
 let triples = measurements.windows(3);
 let mut collection_sums:Vec<u32> = Vec::new();
    for triple in triples {
        let sum = triple.iter().sum();
        collection_sums.push(sum);
    }

    return collection_sums
}
```

```Rust
#[derive(Debug)]
pub enum State {
    Start,
    Transient,
    Closed,
}

impl From<&'a str> for State {
    fn from(s: &'a str) -> Self {
        match s {
            "start" => State::Start,
            "closed" => State::Closed,
            _ => unreachable!(),
        }
    }
}
```

That's it for day one so far. Write me in the comments how exited you are!

<script src="https://cdn.commento.io/js/commento.js"></script>

---

## Navigation

- Parent: [Life, Tech & Adventures: A Journey in Bytes and Boardgames](https://blog.philippludewig.de/de/index.md)
- Previous: [Advent of Code 2021 Day 2](https://blog.philippludewig.de/de/writing/advent-of-code-2021-day-2.md)
- Next: [Let's take a break!](https://blog.philippludewig.de/de/writing/unpaid-leave-timeout.md)
