Juha-Matti Santala

# Advent of Code - 2021

This is a solution to Day 1 of Advent of Code 2021.

## Day 1 - Sonar Sweep

As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.

For example, suppose you had the following report:

``````199
200
208
210
200
207
240
269
260
263
``````

This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.

For reading data, I created a helper function `read_input` in `utils.py` that takes a number as its first argument (for day number) and a function as its second argument.

It then reads the input from file `inputs/day_[number].txt`, reads each line into an item in list (stripping whitespace) and runs the second argument function on each item.

For example, here we want all the inputs to be numeric so we can compare them, so I pass in `int` as the transformer function.

``````from utils import read_input

### Part 1

The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something.

To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.)

To kick off this year's Advent of Code, we're gonna loop over the measurements and see how many times the measurement have compared to the previous one.

``````def count_increases(data):
increase_count = 0
prev = None

for measurement in data:
if prev and measurement > prev:
increase_count += 1

prev = measurement
return increase_count``````

How many measurements are larger than the previous measurement?

``````result = count_increases(depth_measurements)
print(f'There are {result} increased measurements in the data.')``````

There are 1688 increased measurements in the data.

### Part 2

Considering every single measurement isn't as useful as you expected: there's just too much noise in the data.

Instead, consider sums of a three-measurement sliding window. Again considering the above example:

``````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
``````

Start by comparing the first and second three-measurement windows. The measurements in the first window are marked A (`199`, `200`, `208`); their sum is `199 + 200 + 208 = 607`. The second window is marked B (`200`, `208`, `210`); its sum is `618`. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased.

Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum.

``````three_measurement_windows = []

for lower_bound in range(len(depth_measurements)-2):
upper_bound = lower_bound + 3
three_measurement_windows.append(sum(depth_measurements[lower_bound:upper_bound]))

result = count_increases(three_measurement_windows)
print(f'There are {result} increases in three-measurement windows.')
``````

There are 1728 increases in three-measurement windows.

Reading other people's solutions, I really enjoyed akx's way of using deque with Python generators to create a sliding window.

### 2 stars!

And that's a wrap for day 1.

The second part felt a bit more complex than usual day 1 puzzles.