-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
71 lines (55 loc) · 1.8 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# part 1
TEST_INPUT = """0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45""".splitlines()
with open("day9.txt") as f:
TEST_INPUT = f.read().splitlines()
formatted_input = []
for line in TEST_INPUT:
formatted_input.append([int(x) for x in line.split(" ")])
newest_number = 0
current_pattern = []
patterns = []
for pattern in formatted_input:
current_pattern = []
current_pattern.append(pattern)
while not (all(x == 0 for x in current_pattern[-1])):
current_pattern.append([])
for i in range(len(current_pattern[-2]) - 1):
current_pattern[-1].append(
current_pattern[-2][i + 1] - current_pattern[-2][i]
)
patterns.append(current_pattern)
for pattern in patterns:
for i in reversed(range(len(pattern) - 1)):
pattern[i].append(pattern[i][-1] + pattern[i + 1][-1])
newest_number += pattern[0][-1]
print(newest_number)
# part 2
TEST_INPUT = """0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45""".splitlines()
with open("day9.txt") as f:
TEST_INPUT = f.read().splitlines()
formatted_input = []
for line in TEST_INPUT:
formatted_input.append([int(x) for x in line.split(" ")])
newest_number = 0
current_pattern = []
patterns = []
for pattern in formatted_input:
current_pattern = []
current_pattern.append(pattern)
while not (all(x == 0 for x in current_pattern[-1])):
current_pattern.append([])
for i in range(len(current_pattern[-2]) - 1):
current_pattern[-1].append(
current_pattern[-2][i + 1] - current_pattern[-2][i]
)
patterns.append(current_pattern)
newest_number = 0
for pattern in patterns:
for i in reversed(range(len(pattern) - 1)):
pattern[i].insert(0, pattern[i][0] - pattern[i + 1][0])
newest_number += pattern[0][0]
print(newest_number)