-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83e463b
commit 3ba488e
Showing
3 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import itertools | ||
from typing import Iterable | ||
|
||
|
||
def _parse_input(lines: Iterable[str]) -> Iterable[list[int]]: | ||
for line in lines: | ||
yield [int(x) for x in line.strip().split()] | ||
|
||
|
||
def construct_tree_with_diffs(seq: list[int]) -> list[list[int]]: | ||
tree: list[list[int]] = [seq] | ||
seq_to_check = seq | ||
while True: | ||
diff = [b - a for a, b in itertools.pairwise(seq_to_check)] | ||
if all(x == 0 for x in diff): | ||
break | ||
|
||
tree.append(diff) | ||
seq_to_check = diff | ||
return tree | ||
|
||
|
||
def p1(input_str: str) -> int: | ||
input_data = _parse_input(input_str.splitlines()) | ||
|
||
def solve(seq: list[int]) -> int: | ||
tree = construct_tree_with_diffs(seq) | ||
|
||
tree[-1].append(tree[-1][-1]) | ||
for i in reversed(range(len(tree) - 1)): | ||
tree[i].append(tree[i + 1][-1] + tree[i][-1]) | ||
|
||
return tree[0][-1] | ||
|
||
return sum(solve(seq) for seq in input_data) | ||
|
||
|
||
def p2(input_str: str) -> int: | ||
input_data = _parse_input(input_str.splitlines()) | ||
|
||
def solve(seq: list[int]) -> int: | ||
tree = construct_tree_with_diffs(seq) | ||
|
||
tree[-1].insert(0, tree[-1][0]) | ||
for i in reversed(range(len(tree) - 1)): | ||
tree[i].insert(0, tree[i][0] - tree[i + 1][0]) | ||
|
||
return tree[0][0] | ||
|
||
return sum(solve(seq) for seq in input_data) |
Oops, something went wrong.