2021-12-1

Created on Monday, December 23, 2024.

Day 1

We've got a list of depth readings. We need to loop over all the numbers, counting those that are greater than the one before. We need to make sure that we don't count the first number as increasing from "nothing".

## Import ipytest and get it setup for use in Python Notebook
import pytest
import ipytest
ipytest.autoconfig()

We are going to need a function that can iterate over a list of numbers, and count them if they are greater.

It's going to keep track of the last number we looked at. We need to ensure that it starts at the first reading, which we do by setting the "last number" to be equal to the starting number. If we wanted to track the depth increasing with the first number, we'd just start last number at 0 instead.

def count_increases(l):
    count = 0
    lastnumber = l[0]
    for num in l:
        if num > lastnumber:
            count = count + 1
        lastnumber = num
    return count
test_list = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]
assert count_increases(test_list) == 7

Great, that works. Let's try that on the real list.

That means we are going to want to read the lines in from a file and turn them into numbers. This is a common thing for the advent of code, so there's a pretty common pattern that I tend to use.

We need to strip the whitespace like line endings off of each line, and then turn it into an integer.

data = [int(line.strip()) for line in open("day1.txt").readlines()]
assert count_increases(data) == 1624

Great, we've got the answer for part 1, it's 1624 for my dataset!

Onto...

Part 2

We now need a 3 measurement window instead of just the individual readings. We then sum the window and compare the window sums with each other, still looking for an increase in depth.

So for list a, b, c, d, e, we need something that can return (a,b,c) and then (b,c,d) and so on.

We need to end when the start reaches the total - 2, so we get (total-2, total-1, total)

We'll write a window function that returns just the relevant subset of the list. Because it returns a list itself, we can use the sum function to get the total of adding the three numbers together.

def window(l, start):
    return l[start:start+3]

assert window(test_list, 0) == [199, 200, 208]
assert window(test_list, 1) == [200, 208, 210]
assert window(test_list, 7) == [269, 260, 263]

assert sum(window(test_list,0)) == 607

We are going to need a new version of count increases that uses the sliding windows properly.

Most importantly, we need to set the lastnumber equivalent to be the same as the very first window, and we need to make sure that we end 2 before the total number of readings, because we don't want partial windows returned.

def count_increases_sliding(l):
    count = 0
    lastsum = sum(window(l,0))
    for index in range(len(l)-2):
        winsum = sum(window(l, index))
        if winsum > lastsum:
            count = count + 1
        lastsum = winsum
    return count
assert count_increases_sliding(test_list) == 5
assert count_increases_sliding(data) == 1653

Next

2021-12-2

Previous

2020-12-9