Created
December 4, 2021 19:54
-
-
Save Steph-harris/34900d8dbed8de2aa40792b616080cac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
#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