-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
52 lines (41 loc) · 1.05 KB
/
day9.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
43
44
45
46
47
48
49
50
51
52
input = []
def parse_input():
with open("test.txt") as file:
for line in file:
input.append(list(map(int, line.split())))
# /****** Part 1 *******/
def find_next(seq):
hold = [seq[-1]]
while not all(v == 0 for v in seq.copy()):
diffs = []
for n in range(len(seq) - 1):
diffs.append(seq[n + 1] - seq[n])
hold.append(diffs[-1])
seq = diffs
return sum(hold)
def find_sum():
sum = 0
for seq in input:
sum += find_next(seq)
return sum
# /****** Part 2 *******/
def find_previous(seq):
hold = [seq[0]]
prev = 0
while not all(v == 0 for v in seq.copy()):
diffs = []
for n in range(len(seq) - 1):
diffs.append(seq[n + 1] - seq[n])
hold.append(diffs[0])
seq = diffs
for i, n in enumerate(hold):
if i % 2 == 0:
prev = prev + n
else:
prev = prev - n
return prev
def find_previous_sum():
sum = 0
for seq in input:
sum += find_previous(seq)
return sum