Last active
December 15, 2023 10:21
-
-
Save vadim2404/41667ccdf8b529d0666d46455743c0f1 to your computer and use it in GitHub Desktop.
day9
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
#!/usr/bin/env python3 | |
def solve(arr: list[int]) -> int: | |
n = len(arr) | |
x = [arr] | |
for i in range(n - 1): | |
x.append([x[i][j + 1] - x[i][j] for j in range(n - i - 1)]) | |
x[-1].append(0) | |
for i in range(n - 2, -1, -1): | |
x[i].append(x[i][-1] + x[i + 1][-1]) | |
return x[0][-1] | |
with open("input.txt") as f: | |
print(sum(solve([int(x) for x in line.split(" ")]) for line in f)) |
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
#!/usr/bin/env python3 | |
def solve(arr: list[int]) -> int: | |
arr.reverse() | |
n = len(arr) | |
x = [arr] | |
for i in range(n - 1): | |
x.append([x[i][j + 1] - x[i][j] for j in range(n - i - 1)]) | |
x[-1].append(0) | |
for i in range(n - 2, -1, -1): | |
x[i].append(x[i][-1] + x[i + 1][-1]) | |
return x[0][-1] | |
with open("input.txt") as f: | |
print(sum(solve([int(x) for x in line.split(" ")]) for line in f)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment