Skip to content

Instantly share code, notes, and snippets.

@vadim2404
Last active December 15, 2023 10:21
Show Gist options
  • Save vadim2404/41667ccdf8b529d0666d46455743c0f1 to your computer and use it in GitHub Desktop.
Save vadim2404/41667ccdf8b529d0666d46455743c0f1 to your computer and use it in GitHub Desktop.
day9
#!/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))
#!/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