Created
December 1, 2021 05:17
-
-
Save rejoycesong/285d0dfd9307d17fce3e2615b9184419 to your computer and use it in GitHub Desktop.
aoc 2021 - day 1
This file contains hidden or 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
input = open('input/day1.txt', 'r').readlines() | |
numbers = [int(line) for line in input] | |
# O(n) | |
def partOne(numbers=numbers): | |
count = 0 | |
for i in range(1, len(numbers)): | |
if numbers[i] > numbers[i-1]: | |
count += 1 | |
return count | |
# O(n) | |
def partOne_OneLine(numbers=numbers): | |
return sum([1 for i in range(1, len(numbers)) if numbers[i] > numbers[i-1]]) | |
# O(n) | |
def partTwo(numbers=numbers): | |
curr_window = numbers[0] + numbers[1] + numbers[2] | |
count = 0 | |
for i in range(3, len(numbers)): | |
next_window = curr_window - numbers[i-3] + numbers[i] | |
if next_window > curr_window: | |
count += 1 | |
curr_window = next_window | |
return count | |
print("Part One:", partOne()) | |
print("Part One (One-line):", partOne_OneLine()) | |
print("Part Two:", partTwo()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment