-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.py
42 lines (34 loc) · 1.11 KB
/
day09.py
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
39
40
41
42
def part1(task_input):
series = [[int(x) for x in y.split(' ')] for y in task_input.split('\n')]
result = 0
for s in series:
cur = s
extra = [s]
while any(x for x in cur if x != 0):
nxt = []
for i in range(len(cur) - 1):
nxt.append(cur[i + 1] - cur[i])
extra.append(nxt)
cur = nxt
extra[-1].append(0)
for i in range(len(extra) - 2, -1, -1):
extra[i].append(extra[i][-1] + extra[i+1][-1])
result += extra[0][-1]
return result
def part2(task_input):
series = [[int(x) for x in y.split(' ')] for y in task_input.split('\n')]
result = 0
for s in series:
cur = s
extra = [s]
while any(x for x in cur if x != 0):
nxt = []
for i in range(len(cur) - 1):
nxt.append(cur[i + 1] - cur[i])
extra.append(nxt)
cur = nxt
extra[-1].insert(0, 0)
for i in range(len(extra) - 2, -1, -1):
extra[i].insert(0, extra[i][0] - extra[i+1][0])
result += extra[0][0]
return result