-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_9_part_2.py
45 lines (39 loc) · 1.06 KB
/
day_9_part_2.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
from icecream import ic
def get_differences(nums):
differences = []
all_zeros = True
for i in range(1, len(nums)):
difference = nums[i] - nums[i-1]
if all_zeros and difference != 0:
all_zeros = False
differences.append(nums[i] - nums[i-1])
return differences, all_zeros
def main():
ms = []
with open('data/day9.data') as f:
for row in f:
m = []
nums = [int(x) for x in row.split()]
m.append(nums)
cur = nums
while True:
differences, all_zeros = get_differences(cur)
m.append(differences)
if all_zeros:
break
cur = differences
ms.append(m)
ans = 0
for m in ms:
i = len(m) - 1
cur = m[i]
cur_first = 0
while i > 0:
next = m[i-1]
cur_first = next[0] - cur_first
cur = next
i -= 1
ans += cur_first
ic(ans)
if __name__ == '__main__':
main()