-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_09.py
More file actions
38 lines (23 loc) · 927 Bytes
/
Day_09.py
File metadata and controls
38 lines (23 loc) · 927 Bytes
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
36
37
38
from time import perf_counter
start_time = perf_counter()
def open_file(file_name: str = "Day_09.txt") -> str:
with open(file_name) as f:
return f.read()
def differences(line: list[int]) -> list[int]:
return [line[i + 1] - line[i] for i in range(len(line) - 1)]
def extrapolate(line: list[int]) -> list[int]:
if all(x == 0 for x in line):
return 0
return line[-1] + extrapolate(differences(line))
def part_one(lines: list[list[int]]) -> int:
return sum(extrapolate(line) for line in lines)
def part_two(lines: list[list[int]]) -> int:
return sum(extrapolate(line[::-1]) for line in lines)
def main():
lines = open_file().splitlines()
lines = [list(map(int, line.split())) for line in lines]
print("Part 1: ", part_one(lines))
print("Part 2: ", part_two(lines))
if __name__ == "__main__":
main()
print("Time elapsed: ", perf_counter() - start_time)