Skip to content

Instantly share code, notes, and snippets.

@Steph-harris
Created December 4, 2021 19:54
Show Gist options
  • Save Steph-harris/34900d8dbed8de2aa40792b616080cac to your computer and use it in GitHub Desktop.
Save Steph-harris/34900d8dbed8de2aa40792b616080cac to your computer and use it in GitHub Desktop.
"""
#1
Count the number of times a depth measurement increases
from the previous measurement.
(There is no measurement before the first measurement.)
#2
Count the number of times the sum of measurements in a
3 measurement sliding window increases
"""
from collections import deque
from itertools import islice
def main():
with open('../data_fixtures/2021_day_1.txt', "r") as test:
depths = (int(x) for x in test.read().splitlines())
counter = 0
prev = next(depths)
for depth in depths:
if depth > prev:
counter += 1
prev = depth
print(f"counter: {counter}")
# part 2
with open('../data_fixtures/2021_day_1.txt', "r") as test:
depths = (int(x) for x in test.read().splitlines())
prev_window = deque(islice(depths, 3), maxlen=3)
counter = 0
try:
while len(prev_window) == 3:
prev_sum = sum(prev_window)
# push oldest depth out of deque
prev_window.append(next(depths))
curr_sum = sum(prev_window)
if curr_sum > prev_sum:
counter += 1
except StopIteration:
print(f"counter is {counter}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment