Skip to content

Instantly share code, notes, and snippets.

@albertein
Last active December 2, 2021 23:56
Show Gist options
  • Save albertein/1ec91f5c5e88034a618be910ef452746 to your computer and use it in GitHub Desktop.
Save albertein/1ec91f5c5e88034a618be910ef452746 to your computer and use it in GitHub Desktop.
if __name__ == '__main__':
with open('input.txt') as data:
last = None
increased = 0
sum_window = 0
window = []
count = 0
for line in data:
value = int(line)
sum_window += value
# If we don't have the initial window just yet just add the number to the window.
if len(window) < 3:
window.append(value)
if len(window) == 3:
last = sum_window
else:
# We need to substract the last added element from the window to the sum
# so that we get the current sum.
sum_window -= window[count % 3]
# Override last added element on the sliding window with the current value.
window[count % 3] = value
if sum_window > last:
increased += 1
last = sum_window
count += 1
print(increased)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment