From 64624865d6c1a71c6af34b06a69ed0577915358c Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 9 Dec 2023 10:22:00 +0100 Subject: [PATCH] add: part2 --- 2023/day09/src/part2.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2023/day09/src/part2.py diff --git a/2023/day09/src/part2.py b/2023/day09/src/part2.py new file mode 100644 index 0000000..bc25660 --- /dev/null +++ b/2023/day09/src/part2.py @@ -0,0 +1,32 @@ +text: str = open("example.txt", "r").read() + +_value: int = 0 + +lines = text.splitlines() + +def get_difference(values: [int]): + lst = [] + for i in range(0, len(values) - 1): + lst.append(values[i + 1] - values[i]) + return lst + +def get_next(derives: [[int]]): + value = 0 + for values in derives[::-1]: + if (len(values) == 0): + continue + value = values[0] - value + return value + +for line in lines: + values = list(map(int, line.split())) + derives: [[int]] = [values] + lst = values + while True: + lst = get_difference(lst) + derives.append(lst) + if (lst.count(0) == len(lst)): + break + _value += get_next(derives) + +print(_value) \ No newline at end of file