-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle-9.py
63 lines (50 loc) · 1.43 KB
/
Puzzle-9.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
from collections import OrderedDict
from collections import namedtuple
from collections import Counter
import numpy as np
test = """0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
"""
def diffPyramideLast(v):
"""
np.array[int] -> int
"""
cvec = v.copy()
valcum = cvec[-1]
for i in range(len(v)-1):
#print(cvec)
nvec = np.diff(cvec)
valcum -= nvec[-1]
cvec = nvec.copy()
if np.all(nvec ==0):
break
return valcum
def diffPyramideFirst(v):
"""
np.array[int] -> int
"""
cvec = v.copy()
l_vals = [cvec[0]]
for i in range(len(v)-1):
#print(cvec)
nvec = np.diff(cvec)
l_vals.append(nvec[0])
cvec = nvec.copy()
if np.all(nvec ==0):
break
l_first_vals = [0] * len(l_vals)
for i in reversed(range(len(l_vals)-1)):
l_first_vals[i] = l_vals[i] - l_first_vals[i+1]
return l_first_vals[0]
l_test = test.split("\n")
l_tvecs = [np.array([int(y) for y in x.split(" ")]) for x in l_test if len(x)>0]
test_first = [diffPyramideFirst(x) for x in l_tvecs]
filename = "./input_day9.txt"
fin = open(filename, "r")
textfull = fin.read()
fin.close()
all_text = [x for x in textfull.split("\n") if len(x) >0]
l_vecs = [np.array([int(y) for y in x.split(" ")]) for x in all_text if len(x)>0]
all_proj_last = [diffPyramideLast(x) for x in l_vecs]
all_proj_first = [diffPyramideFirst(x) for x in l_vecs]