Skip to content

Instantly share code, notes, and snippets.

@raeq
Created December 1, 2021 13:10
Show Gist options
  • Save raeq/431f55759cb5b493d690cb948e823d92 to your computer and use it in GitHub Desktop.
Save raeq/431f55759cb5b493d690cb948e823d92 to your computer and use it in GitHub Desktop.
import more_itertools
def get_data(filename: str) -> list:
with open(filename, 'r') as f:
return [int(line.rstrip('\n')) for line in f]
def make_couples(singles: list) -> list:
return zip(singles, singles[1:])
def count_increases(couples: list) -> int:
return sum(next > prev for prev, next in couples)
if __name__ == "__main__":
data = get_data("day01.txt")
depths_couples = make_couples(data)
num_increases = count_increases(depths_couples)
print(f'Day 1 Star 1 answer: {num_increases}')
triples = [sum(triple) for triple in more_itertools.triplewise(data)]
windowed_couples = make_couples(triples)
num_increases = count_increases(windowed_couples)
print(f'Day 1 Star 2 answer: {num_increases}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment