Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2021-12-01 - Sonar Sweep

(original .ipynb)

Day 1 puzzle input is a list of integers which represent a sequence of depth readings (mine is here). Part 1 involves counting how many individual readings were larger than the previous one. Part 2 is the exact same but for the sum of a "depth window" of the previous three readings.

import math

puzzle_input_str = open("puzzle_input/day1.txt").read()

test_input_str = """199
200
208
210
200
207
240
269
260
263"""


def parse_input(input_str):
    return [int(depth_str) for depth_str in input_str.split("\n")]


def times_window_increased(depths, window_size):
    prev_window = math.inf
    index = 0
    count = 0
    while index + window_size <= len(depths):
        window = sum(depths[index:index+window_size])
        if window > prev_window:
            count += 1
        
        prev_window = window
        index += 1
    
    return count


def part_one(input_str):
    depths = parse_input(input_str)
    return times_window_increased(depths, 1)


assert 7 == part_one(test_input_str)
print("part one:", part_one(puzzle_input_str))
part one: 1559
def part_two(input_str):
    depths = parse_input(input_str)
    return times_window_increased(depths, 3)


assert 5 == part_two(test_input_str)
print("part two:", part_two(puzzle_input_str))
part two: 1600