AdventOfCode/2023/day9/part2.py

43 lines
834 B
Python
Raw Normal View History

2023-12-09 19:30:11 +01:00
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)
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 = []
2023-12-09 19:41:13 +01:00
left = []
2023-12-09 19:30:11 +01:00
for mdx,m in enumerate(maps):
t = []
last = []
last.append(m[-1])
last.append(calc(m))
result.append(reduce(lambda x, y: x+y, last))
2023-12-09 19:41:13 +01:00
tx = []
tmp = 0
for i in range(len(t)-1,-1,-1):
tx.append(t[i][0])
tmp = t[i][0] - tmp
left.append(tmp)
2023-12-09 19:30:11 +01:00
2023-12-09 19:41:13 +01:00
print('Part 1: ' + str(sum(result)))
print('Part 2: ' + str(sum(left)))
2023-12-09 19:30:11 +01:00