From e2ecfaedadb4ae290e198574ead757c7f8a9b2db Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sat, 9 Dec 2023 19:30:11 +0100 Subject: [PATCH] Working on 2023-12-09 p2 --- 2023/day9/part2.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 2023/day9/test | 3 +++ 2 files changed, 53 insertions(+) create mode 100644 2023/day9/part2.py create mode 100644 2023/day9/test diff --git a/2023/day9/part2.py b/2023/day9/part2.py new file mode 100644 index 0000000..5b679d1 --- /dev/null +++ b/2023/day9/part2.py @@ -0,0 +1,50 @@ +import sys +from pprint import pprint +from functools import reduce + +maps = [] + +with open(sys.argv[1]) as file: + for line in file: + maps.append([int(x) for x in line.rstrip().split(' ')]) + +t = [] + +def calc(line): + global t + t.append(line) + print(line) + diff = [] + for i in range(0,len(line)-1): + diff.append(line[i+1]-line[i]) + if not all(v == 0 for v in diff): + last.append(diff[-1]) + calc(diff) + else: + print(diff) + return 0 + +prev = 0 + +result = [] +for mdx,m in enumerate(maps): + t = [] + left = [] + last = [] + last.append(m[-1]) + last.append(calc(m)) + result.append(reduce(lambda x, y: x+y, last)) + print(t) +pprint(last) +print(sum(result)) +pprint(t) + +tx = [] + +tmp = 0 +for i in range(len(t)-1,-1,-1): + tx.append(t[i][0]) + tmp = t[i][0] - tmp + +print(tx) +print(tmp) diff --git a/2023/day9/test b/2023/day9/test new file mode 100644 index 0000000..539a763 --- /dev/null +++ b/2023/day9/test @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45