Part 1:
The task involves analyzing an environmental report from an oasis using the Oasis And Sand Instability Sensor (OASIS). The report consists of multiple histories, each containing a sequence of values over time. The goal is to predict the next value in each history by creating a sequence of differences at each step. This process is repeated until a sequence of zeroes is obtained, and the next values are extrapolated. The sum of these extrapolated values is the solution.
For example:
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
In the above dataset, the first history is 0 3 6 9 12 15. Because the values increase by 3 each step, the first sequence of differences that you generate will be 3 3 3 3 3. Note that this sequence has one fewer value than the input sequence because at each step it considers two numbers from the input. Since these values aren't all zero, repeat the process: the values differ by 0 at each step, so the next sequence is 0 0 0 0. This means you have enough information to extrapolate the history! Visually, these sequences can be arranged like this:
0 3 6 9 12 15
3 3 3 3 3
0 0 0 0
To extrapolate, start by adding a new zero to the end of your list of zeroes; because the zeroes represent differences between the two values above them, this also means there is now a placeholder in every sequence above it:
0 3 6 9 12 15 B
3 3 3 3 3 A
0 0 0 0 0
You can then start filling in placeholders from the bottom up. A needs to be the result of increasing 3 (the value to its left) by 0 (the value below it); this means A must be 3:
0 3 6 9 12 15 B
3 3 3 3 3 3
0 0 0 0 0
Finally, you can fill in B, which needs to be the result of increasing 15 (the value to its left) by 3 (the value below it), or 18:
0 3 6 9 12 15 18
3 3 3 3 3 3
0 0 0 0 0
So, the next value of the first history is 18.
#!/usr/bin/env python3
from pathlib import Path
from typing import Iterable
import typer
def find_next_val(line: str) -> int:
# N1 N2 N3 N4 ...
seqs = [list(map(int, line.split()))]
while True:
new = [-1 * (seqs[-1][i] - seqs[-1][i + 1]) for i in range(len(seqs[-1]) - 1)]
seqs.append(new)
if all(val == 0 for val in new):
break
last = 0
for seq in reversed(seqs[:-1]):
seq.append(last := seq[-1] + last)
return seqs[0][-1]
def total_values(lines: Iterable[str]) -> int:
return sum(map(find_next_val, lines))
def main(history_file: Path) -> None:
with open(history_file) as f:
print(total_values(f))
if __name__ == "__main__":
typer.run(main)
Part 2:
In Part Two, the task extends to extrapolating values backward in time for each history. The process involves adding a zero to the beginning of the sequence of zeroes and filling in new first values for each previous sequence. The goal is to determine the previous values for each history and calculate their sum.
In particular, here is what the third example history looks like when extrapolating back in time:
5 10 13 16 21 30 45
5 3 3 5 9 15
-2 0 2 4 6
2 2 2 2
0 0 0
Adding the new values on the left side of each sequence from bottom to top eventually reveals the new left-most history value: 5.
Doing this for the remaining example data above results in previous values of -3 for the first history and 0 for the second history. Adding all three new values together produces 2.
#!/usr/bin/env python3
from pathlib import Path
from typing import Iterable
import typer
def find_next_val(line: str) -> int:
# N1 N2 N3 N4 ...
seqs = [list(map(int, line.split()))]
while True:
seqs.append(
[-1 * (seqs[-1][i] - seqs[-1][i + 1]) for i in range(len(seqs[-1]) - 1)]
)
if all(val == 0 for val in seqs[-1]):
break
last = 0
for seq in reversed(seqs[:-1]):
seq.insert(0, last := seq[0] - last)
return seqs[0][0]
def total_values(lines: Iterable[str]) -> int:
return sum(map(find_next_val, lines))
def main(history_file: Path) -> None:
with open(history_file) as f:
print(total_values(f))
if __name__ == "__main__":
typer.run(main)
Review Request:
General coding comments, style, etc.
What are some possible simplifications? What would you do differently?