-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.py
More file actions
300 lines (265 loc) · 10.8 KB
/
Matrix.py
File metadata and controls
300 lines (265 loc) · 10.8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
import helpers
import itertools
class Matrix:
def __init__(self, browser, size, cellCssClass='puzzlecel'):
self.browser = browser
self.size = size
self.cellCssClass = cellCssClass
self.vectorTypes = ('row', 'col')
self.totalCount = 0
self.count = {}
for v in self.vectorTypes:
self.count[v] = []
for i in range(size):
self.count[v].append(
{
'total': 0,
0: 0,
1: 0
}
)
self.completeVectors = []
self.nearCompleteVectors = []
# We can guess up to this many missing cells
self.maxComboSize = 7
self.load()
self.countRowsAndCols()
self.updateCompleteness()
# Load matrix from game with given URL
def load(self):
cells = self.browser.find_elements_by_class_name(self.cellCssClass)
self.values = []
for i in range(self.size):
row = []
for j in range(self.size):
stringValue = cells[i*self.size+j].text
intValue = int(stringValue) if stringValue.strip() else None
row.append(intValue)
self.values.append(row)
# Print the current matrix
def print(self):
print('Printing matrix...')
for i in range(self.size):
row = []
for j in range(self.size):
row.append(self.values[i][j])
print(row)
print()
# Draw the matrix on browser
def draw(self):
for i in range(self.size):
for j in range(self.size):
value = self.values[i][j]
cellCssId = '#cel_' + str(i+1) + '_' + str(j+1)
cell = self.browser.find_element_by_css_selector(cellCssId)
helpers.drawCell(self.browser, cell, value)
# Set cell at given position to given value
def addCount(self, i, j, value):
# Ignore none value
if value is None:
return
self.totalCount += 1
self.count['row'][i][value] += 1
self.count['row'][i]['total'] += 1
self.count['col'][j][value] += 1
self.count['col'][j]['total'] += 1
self.updateVectorCompleteness('row', i)
self.updateVectorCompleteness('col', j)
# Set cell value and add count
def setCell(self, i, j, value):
if value is None:
return
if self.values[i][j] is not None:
return
self.values[i][j] = value
self.addCount(i, j, value)
cellCssId = '#cel_' + str(i+1) + '_' + str(j+1)
cell = self.browser.find_element_by_css_selector(cellCssId)
helpers.drawCell(self.browser, cell, value)
# Check if the matrix is complete
def isComplete(self):
return self.totalCount == self.size * self.size
# Check if the matrix solution is correct
def isCorrect(self):
if not self.isComplete():
return False
for i in range(self.size):
for v in self.vectorTypes:
if (self.count[v][i]['total'] != self.size or
self.count[v][i][0] != self.size/2 or
self.count[v][i][1] != self.size/2):
print(v + ' ' + str(i)
+ ' has ' + str(self.count[v][i][0]) + ' zeros'
+ ' and ' + str(self.count[v][i][1]) + ' ones')
return False
return True
# Check if index is in range
def indexIsInRange(self, row, col):
if row < 0 or row > self.size-1:
return False
if col < 0 or col > self.size-1:
return False
return True
# Set the adjacent cells of neighbours to the other number
def setNeighbours(self, neighbours, i, j):
current = self.values[i][j]
for neighbour in neighbours:
row = neighbour['row']
col = neighbour['col']
# If neighbour exists and equal to the current cell
if (self.indexIsInRange(row, col) and
self.values[row][col] == current):
for adjacentCell in neighbour['adjacentCells']:
adjRow = adjacentCell['row']
adjCol = adjacentCell['col']
if (self.indexIsInRange(adjRow, adjCol) and
self.values[adjRow][adjCol] is None):
# Set the adjcent to the other number
print('Cell (' + str(adjRow) + ', ' + str(adjCol) +
') is set to ' + str(1 - current) +
' because both cells ' +
'(' + str(i) + ', ' + str(j) + ')' + ' and ' +
'(' + str(row) + ', ' + str(col) + ')' +
' are ' + str(current))
self.setCell(adjRow, adjCol, 1 - current)
# Count the not none cells in rows/columns
def countRowsAndCols(self):
for i in range(self.size):
for j in range(self.size):
if self.values[i][j] is not None:
self.addCount(i, j, self.values[i][j])
# Check for complete and near complete rows/columns
def updateCompleteness(self):
for i in range(self.size):
self.updateVectorCompleteness('row', i)
self.updateVectorCompleteness('col', i)
# Check for complete and near complete for current row/column
def updateVectorCompleteness(self, vectorType, i):
vectorMissingCells = self.size - self.count[vectorType][i]['total']
vector = (vectorType, i, vectorMissingCells)
# This vector is complete
if vectorMissingCells == 0:
if self.checkCorrectness(vectorType, i) is False:
print(vector)
raise Exception(vectorType + ' ' + str(i) + ' is not correct!')
print(vectorType + ' ' + str(i) + ' is complete')
# Add to complete vector list if does not exist
if vector not in self.completeVectors:
print('adding to the complete vectors')
self.completeVectors.append(vector)
# Remove from near complete vector list if exists
self.nearCompleteVectors = list(filter(
lambda v: not (v[0] == vectorType and v[1] == i),
self.nearCompleteVectors))
# This vector is near complete
elif vectorMissingCells <= self.maxComboSize:
oldVector = None
for (vv, ii, mm) in self.nearCompleteVectors:
if vv == vectorType and ii == i:
oldVector = (vv, ii, mm)
if oldVector is not None:
self.nearCompleteVectors.remove(oldVector)
self.nearCompleteVectors.append(vector)
# Sort by number of missing cells
self.nearCompleteVectors.sort(key=lambda row: row[2])
# Check if vector is correct
# Aka, having equal number of zeros and ones and no missing element
def checkCorrectness(self, vectorType, i):
if self.count[vectorType][i]['total'] != self.size:
return False
if self.count[vectorType][i][0] != self.size/2:
return False
if self.count[vectorType][i][1] != self.size/2:
return False
return True
# Generate all permutations given number of ones and zeros
def getCombo(self, zeros, ones):
zeros = int(zeros)
ones = int(ones)
return list(set(list(itertools.permutations([0]*zeros + [1]*ones))))
# Get row/col candidates
def getCandidates(self, vectorType, i, missingCount):
candidates = []
combos = self.getCombo(
self.size/2 - self.count[vectorType][i][0],
self.size/2 - self.count[vectorType][i][1])
for combo in combos:
count = 0
candidate = []
for j in range(self.size):
(row, col) = self.getRowAndColIndexes(vectorType, i, j)
# Load from combo
if self.values[row][col] is None:
val = combo[count]
count += 1
isGuess = True
# Load from current cell
else:
val = self.values[row][col]
isGuess = False
candidate.append({
'row': row,
'col': col,
'val': val,
'isGuess': isGuess
})
candidates.append(candidate)
return candidates
# Get row and col indexes given vector type
def getRowAndColIndexes(self, vectorType, i, j):
if vectorType == 'row':
return (i, j)
elif vectorType == 'col':
return (j, i)
else:
raise Exception('Unknown vector type!')
# Check if it violates rules
def violatesRules(self, vectorType, candidate):
vector = []
for x in range(len(candidate)):
vector.append(candidate[x]['val'])
if self.hasThreeOrMoreConsecutiveSameNumber(vector):
return True
for j in range(len(candidate)):
if candidate[j]['isGuess']:
crossVector = []
for i in range(self.size):
if i == candidate[j][vectorType]:
crossVector.append(candidate[j]['val'])
else:
(row, col) = self.getRowAndColIndexes(vectorType, i, j)
crossVector.append(self.values[row][col])
if self.hasThreeOrMoreConsecutiveSameNumber(crossVector):
return True
return False
# Check if it has consecutive three or more zeros/ones
def hasThreeOrMoreConsecutiveSameNumber(self, vector):
previous = None
streak = 0
for x in range(len(vector)):
current = vector[x]
if current == previous and current is not None:
streak += 1
if streak >= 2:
return True
else:
streak = 0
previous = current
return False
# Check if matrix has duplicated rows/cols
def hasDuplicatedVectors(self, vectorType, target):
# Get complete vectors of the same type
sources = list(filter(
lambda x: x[0] == vectorType, self.completeVectors)
)
# Nothing to compare against
if len(sources) == 0:
return False
# Check target against every source
for (v, i, m) in sources:
for j in range(len(target)):
(row, col) = self.getRowAndColIndexes(vectorType, i, j)
if target[j]['val'] != self.values[row][col]:
return False
return True