-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_09.py
35 lines (25 loc) · 1.05 KB
/
day_09.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
--- Day 9: Mirage Maintenance ---
"""
from typing import List
def all_zero(history: List[int]) -> bool:
return all(step == 0 for step in history)
def find_differences(history: List[int]) -> List[int]:
return [y - x for x, y in zip(history, history[1:])]
with open("09.in", "r", encoding="utf-8") as f:
report = [
[int(step) for step in history.split()]
for history in f.read().strip().splitlines()
]
p1_extrapolated, p2_extrapolated = 0, 0
for history in report:
sequences_of_differences = [find_differences(history)]
while not all_zero(sequences_of_differences[-1]):
sequences_of_differences.append(find_differences(sequences_of_differences[-1]))
predicted_next, predicted_before = [0], [0]
for seq in reversed([history] + sequences_of_differences[:-1]):
predicted_next.append(seq[-1] + predicted_next[-1])
predicted_before.append(seq[0] - predicted_before[-1])
p1_extrapolated += predicted_next[-1]
p2_extrapolated += predicted_before[-1]
print(p1_extrapolated, p2_extrapolated)