-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
202 lines (183 loc) · 6.5 KB
/
day10.py
File metadata and controls
202 lines (183 loc) · 6.5 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import re
import numpy as np
import math
def main():
lines = open('data/day10.txt', 'r').readlines()
regex = re.compile(r"""\[([.#]*)]+ ([\d,\(\)\s]+) {([\d,]+)}""")
res = 0
res2 = 0
for line in lines:
strState = regex.match(line)[1]
strToggles = regex.match(line)[2]
strVoltages = regex.match(line)[3]
toggleList = [[int(d) for d in c] for c in [b.split(',') for b in [a[1:-1] for a in strToggles.split(' ')]]]
node = '.'*len(strState)
visited = {}
visited[node] = 0
queue = [node]
while len(queue) > 0:
node = queue.pop(0)
for toggle in toggleList:
neigh = makeNeigh(node, toggle)
if neigh in visited:
continue
visited[neigh] = visited[node] + 1
queue.append(neigh)
if neigh == strState:
queue = []
res += visited[neigh]
break
voltageList = [int(a) for a in strVoltages.split(',')]
A = np.array([[0.0 for x in toggleList] for y in voltageList])
for i in range(len(toggleList)):
for t in toggleList[i]:
A[t][i] = 1
voltageList = np.transpose(np.array([voltageList]))
# happy case: we have n equations, n unknown, solve is done
cansolve = True
try:
x = np.linalg.solve(A, voltageList)
except:
cansolve = False
if cansolve:
res2 += round(sum(x)[0])
continue
clicks = sum(voltageList)[0] + 20
voltMax = round(max(voltageList)[0]+1)
(G, V) = gauss(A, voltageList)
# 31101 too high
# nope on 29545
# nope on 20692
# nope on 20696
# nope on 20687
# nope on 20694
# nope on 20695
# nope on 20880
indices = []
for i in range(len(G[0])):
if i - len(indices) < len(G) and G[i-len(indices)][i] == 1:
continue
indices.append(i)
free = len(indices)
if free > 3:
raise Exception('aaaa')
sus = True
for i3 in range(voltMax+1 if free > 2 else 1):
for i2 in range(voltMax+1 if free > 1 else 1):
for i in range(voltMax+1 if free > 0 else 1):
if i + i2 + i3 > voltMax:
continue
D = V.copy()
if free > 0:
D = D - i*np.transpose([np.array(G[..., indices[0]])])
if free > 1:
D = D - i2*np.transpose([np.array(G[..., indices[1]])])
if free > 2:
D = D - i3*np.transpose([np.array(G[..., indices[2]])])
valid = True
cand = 0
D = D.flatten()
for d in range(len(D)):
if D[d] < -0.000000001:
valid = False
break
if abs(round(D[d]) - D[d]) > 0.000000001:
valid = False
break
D[d] = round(D[d])
nonZero = False
for k in range(len(G[d])):
if k not in indices:
if G[d][k] != 0 and G[d][k] != 1:
print('sus')
print(G)
exit()
if round(G[d][k]) != 0:
nonZero = True
if nonZero:
cand += D[d]
cand += i + i2 + i3
if valid and cand < clicks:
sus = False
S = []
t = 0
ts = 0
while True:
if t >= len(A[0]):
break
if t in indices:
if t == indices[0]:
S.append(i)
elif t == indices[1]:
S.append(i2)
else:
S.append(i3)
else:
S.append(round(D[ts]))
ts += 1
t += 1
S = np.transpose(np.array([S]))
if not np.all(np.matmul(A, S) == voltageList):
print('not a solution')
print(line)
print(A)
print(S)
print(G)
print(V)
print(D)
print(voltageList)
print(indices, i, i2, i3)
exit()
clicks = round(cand)
if sus:
print('sus')
print(G)
print(V)
exit()
res2+=clicks
print(res)
print(res2)
def gauss(A, B):
aug = np.hstack((A, B))
h = 0
k = 0
while h < len(aug) and k < len(aug[0]):
col = np.ndarray.flatten(aug[:, k])
imax = -1
valp = 0
for i in range(h, len(col)):
if abs(col[i]) > valp:
valp = abs(col[i])
imax = i
if valp == 0:
k += 1
continue
aug[[imax, h]] = aug[[h, imax]]
aug[h] = aug[h] / aug[h][k]
for i in range(len(aug)):
if (i != h):
aug[i] = aug[i] - aug[h] * aug[i][k]
for i in range(len(aug)):
for j in range(len(aug[i])):
if abs(round(aug[i][j]) - aug[i][j]) < 0.0000001:
aug[i][j] = round(aug[i][j])
h += 1
k += 1
newV = aug[:, -1:]
C = np.delete(aug, len(aug[0]) - 1, axis = 1)
return (C, newV)
def hamming(X, Y):
dist = 0
for i in range(len(X)):
if X[i] != Y[i]:
dist += 1
return dist
def makeNeigh(node, toggle):
neigh = ''
for i in range(0, len(node)):
if i in toggle:
neigh += '.' if node[i] == '#' else '#'
else:
neigh += node[i]
return neigh
main()