From 7b4c3e35364bd6d9e6e4e17db27294656c7f9a8b Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sat, 9 Dec 2023 13:10:34 +0100 Subject: [PATCH] Finished 2023-12-09 p1 --- 2023/day9/part1.py | 27 +++++++++++++++++++++++++++ 2023/day9/t2 | 1 + 2 files changed, 28 insertions(+) create mode 100644 2023/day9/part1.py create mode 100644 2023/day9/t2 diff --git a/2023/day9/part1.py b/2023/day9/part1.py new file mode 100644 index 0000000..df044bb --- /dev/null +++ b/2023/day9/part1.py @@ -0,0 +1,27 @@ +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(' ')]) + +def calc(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) + return 0 +result = [] +for mdx,m in enumerate(maps): + last = [] + last.append(m[-1]) + last.append(calc(m)) + result.append(reduce(lambda x, y: x+y, last)) + +print(sum(result)) + diff --git a/2023/day9/t2 b/2023/day9/t2 new file mode 100644 index 0000000..c7b0638 --- /dev/null +++ b/2023/day9/t2 @@ -0,0 +1 @@ +0 3 6 9 12 15