2023/day09.py

26 lines
622 B
Python
Raw Normal View History

2023-12-29 21:03:50 +00:00
import fileinput
from collections import deque
def main():
part1 = 0
part2 = 0
for line in fileinput.input():
values = deque(int(t) for t in line.split())
stack = [values]
while any(values):
values = deque(values[i + 1] - values[i] for i in range(len(values) - 1))
stack.append(values)
for i in range(len(stack) - 1, 0, -1):
stack[i - 1].append(stack[i - 1][-1] + stack[i][-1])
stack[i - 1].appendleft(stack[i - 1][0] - stack[i][0])
part1 += stack[0][-1]
part2 += stack[0][0]
print(part1, part2)
main()