-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreSolver.py
More file actions
367 lines (315 loc) · 12.3 KB
/
CoreSolver.py
File metadata and controls
367 lines (315 loc) · 12.3 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import heapq
import GameofCrownsBoardFetcher
testBoard = [
['C1', 'C1', 'C2', 'C2', 'C3', 'C4', 'C4'] ,
['C1', 'C2', 'C2', 'C2', 'C2', 'C2', 'C4'] ,
['C1', 'C5', 'C5', 'C5', 'C5', 'C4', 'C4'] ,
['C5', 'C5', 'C6', 'C6', 'C5', 'C5', 'C4'] ,
['C6', 'C6', 'C6', 'C6', 'C5', 'C4', 'C4'] ,
['C6', 'C6', 'C6', 'C6', 'C5', 'C5', 'C5']
]
testBoard2 = [
['C1', 'C1', 'C1', 'C1', 'C1', 'C2', 'C2', 'C2', 'C3'] ,
['C4', 'C5', 'C5', 'C5', 'C1', 'C5', 'C5', 'C5', 'C3'] ,
['C4', 'C5', 'C6', 'C5', 'C1', 'C5', 'C7', 'C5', 'C3'] ,
['C4', 'C5', 'C6', 'C5', 'C5', 'C5', 'C8', 'C5', 'C3'] ,
['C4', 'C5', 'C6', 'C6', 'C8', 'C8', 'C8', 'C5', 'C3'] ,
['C4', 'C5', 'C6', 'C5', 'C5', 'C5', 'C8', 'C5', 'C3'] ,
['C4', 'C5', 'C6', 'C5', 'C9', 'C5', 'C8', 'C5', 'C9'] ,
['C4', 'C5', 'C5', 'C5', 'C9', 'C5', 'C5', 'C5', 'C9'] ,
['C4', 'C4', 'C4', 'C9', 'C9', 'C9', 'C9', 'C9', 'C9']
]
testBoard3 = [
['C1', 'C1', 'C1', 'C1', 'C1', 'C2', 'C2'] ,
['C1', 'C3', 'C3', 'C3', 'C3', 'C3', 'C2'] ,
['C1', 'C3', 'C4', 'C4', 'C4', 'C3', 'C2'] ,
['C1', 'C3', 'C3', 'C3', 'C3', 'C3', 'C2'] ,
['C5', 'C3', 'C6', 'C6', 'C6', 'C3', 'C2'] ,
['C5', 'C3', 'C3', 'C3', 'C3', 'C3', 'C2'] ,
['C5', 'C7', 'C7', 'C2', 'C2', 'C2', 'C2']
]
def get_color_map(board):
d = {}
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] not in d:
d[board[i][j]] = []
d[board[i][j]].append([i,j])
return d
def check_and_append(ans, x, y, board):
if x>=0 and x<len(board) and y>=0 and y<len(board[0]):
ans.append([x,y])
return ans
def get_xs_for_one_cell(x, y, board):
height = len(board)
width = len(board[0])
ans = []
for i in range(width):
if i != y:
ans.append([x,i])
for i in range(height):
if i != x:
ans.append([i,y])
ans = check_and_append(ans, x-1, y-1, board)
ans = check_and_append(ans, x-1, y, board)
ans = check_and_append(ans, x-1, y+1, board)
ans = check_and_append(ans, x, y-1, board)
ans = check_and_append(ans, x, y+1, board)
ans = check_and_append(ans, x+1, y-1, board)
ans = check_and_append(ans, x+1, y, board)
ans = check_and_append(ans, x+1, y+1, board)
return ans
def get_xs_for_one_color(color, colorMap, board):
s = set()
coords = colorMap[color]
# get x for first point of a color
if len(coords) == 0:
return s
xs = get_xs_for_one_cell(coords[0][0], coords[0][1], board)
xs = set(map(tuple, xs))
s = s | xs
for i in range(1, len(coords)):
xs = get_xs_for_one_cell(coords[i][0], coords[i][1], board)
xs = set(map(tuple, xs))
s = s & xs
# draw(list(s), board)
# for i in s:
# x,y = i
# color = board[x][y]
# if [x, y] in colorMap[color]:
# colorMap[color].remove([x,y])
# eliminateCells(s, colorMap, board)
return s
def draw_color_map(colorMap, board, draw = True):
rows, cols = len(board), len(board[0])
drawBoard = [['X ' for _ in range(cols)] for _ in range(rows)]
for color in colorMap:
for i in colorMap[color]:
x,y = i
drawBoard[x][y] = color + ' '
if draw:
for row in drawBoard:
print(''.join(row))
print()
return drawBoard
def draw(l, board, c='X'):
rows, cols = len(board), len(board[0])
drawBoard = [['O' for _ in range(cols)] for _ in range(rows)]
for x, y in l:
drawBoard[x][y] = c
for row in drawBoard:
print(''.join(row))
print()
def eliminate_cells(s, colorMap, board):
numberOfCellsEliminated = 0
for i in s:
x,y = i
color = board[x][y]
if [x, y] in colorMap[color]:
colorMap[color].remove([x,y])
numberOfCellsEliminated += 1
return numberOfCellsEliminated
def get_color_count(colorMap):
c = {}
for i in colorMap:
c[i] = len(colorMap[i])
return c
def verify_board(colorMap, board):
xcord = set()
ycord = set()
for color in colorMap:
if len(colorMap[color]) != 1:
return False
xcord.add(colorMap[color][0][0])
ycord.add(colorMap[color][0][1])
if len(xcord) != len(board) or len(ycord) != len(board[0]):
return False
return True
def eliminate_row_column_surrounding_cells_for_each_color(colorMap, board):
colorCount = get_color_count(colorMap)
pq = []
for c in colorCount:
heapq.heappush(pq, (colorCount[c], c))
while len(pq) > 0:
prio, col = heapq.heappop(pq)
s = get_xs_for_one_color(col, colorMap, board)
nEli = eliminate_cells(s, colorMap, board) #number of cells eliminated
if nEli == 0:
continue
colorCount = get_color_count(colorMap)
# print(colorCount)
pq = []
for c in colorCount:
if colorCount[c]!=1 and c!=col:
heapq.heappush(pq, (colorCount[c], c))
else:
s = get_xs_for_one_color(c, colorMap, board)
eliminate_cells(s, colorMap, board)
def eliminate_by_contained_regions(colorMap, board):
rows, cols = len(board), len(board[0])
for row_subset_size in range(1, rows + 1):
for start_row in range(rows - row_subset_size + 1):
end_row = start_row + row_subset_size
colors_in_rows = set()
for i in range(start_row, end_row):
for j in range(cols):
colors_in_rows.add(board[i][j])
contained_colors = []
for color in colors_in_rows:
all_in_range = True
for pos in colorMap[color]:
x, y = pos
if x < start_row or x >= end_row:
all_in_range = False
break
if all_in_range and len(colorMap[color]) > 0:
contained_colors.append(color)
if len(contained_colors) == row_subset_size and len(contained_colors) > 0:
for i in range(start_row, end_row):
for j in range(cols):
color = board[i][j]
if color not in contained_colors:
if [i, j] in colorMap[color]:
if len(colorMap[color]) > 1:
colorMap[color].remove([i, j])
for col_subset_size in range(1, cols + 1):
for start_col in range(cols - col_subset_size + 1):
end_col = start_col + col_subset_size
colors_in_cols = set()
for i in range(rows):
for j in range(start_col, end_col):
colors_in_cols.add(board[i][j])
contained_colors = []
for color in colors_in_cols:
all_in_range = True
for pos in colorMap[color]:
x, y = pos
if y < start_col or y >= end_col:
all_in_range = False
break
if all_in_range and len(colorMap[color]) > 0:
contained_colors.append(color)
if len(contained_colors) == col_subset_size and len(contained_colors) > 0:
for i in range(rows):
for j in range(start_col, end_col):
color = board[i][j]
if color not in contained_colors:
if [i, j] in colorMap[color]:
if len(colorMap[color]) > 1:
colorMap[color].remove([i, j])
def apply_line_complete_region_elimination(colorMap, board):
rows, cols = len(board), len(board[0])
for i in range(rows):
row_colors = set()
for j in range(cols):
row_colors.add(board[i][j])
if len(row_colors) == 1:
col = row_colors.pop()
for i1 in range(rows):
if i1 == i:
continue
for j1 in range(cols):
if col == board[i1][j1]:
if [i1, j1] in colorMap[col]:
colorMap[col].remove([i1, j1])
for j in range(cols):
col_colors = set()
for i in range(rows):
col_colors.add(board[i][j])
if len(col_colors) == 1:
col = col_colors.pop()
for j1 in range(cols):
if j1 == j:
continue
for i1 in range(rows):
if col == board[i1][j1]:
if [i1, j1] in colorMap[col]:
colorMap[col].remove([i1, j1])
def apply_single_viable_cell_in_line(colorMap, board):
rows, cols = len(board), len(board[0])
drawboard = draw_color_map(colorMap, board, False)
xcoords = {}
ycoords = {}
for color in colorMap:
for pos in colorMap[color]:
x, y = pos
if x not in xcoords:
xcoords[x] = []
if y not in ycoords:
ycoords[y] = []
xcoords[x].append((color, pos))
ycoords[y].append((color, pos))
for x in xcoords:
if len(xcoords[x]) == 1:
color, pos = xcoords[x][0]
colorMap[color] = [pos]
for y in ycoords:
if len(ycoords[y]) == 1:
color, pos = ycoords[y][0]
colorMap[color] = [pos]
def color_map_to_queens(color_map):
queens = []
for region, positions in color_map.items():
for row, col in positions:
queens.append({
"row": int(row),
"col": int(col),
"region": region
})
return queens
def solve(board, printSteps=False):
colorMap = get_color_map(board)
if printSteps:
draw_color_map(colorMap, board)
apply_line_complete_region_elimination(colorMap, board)
colorCount = sum(get_color_count(colorMap).values())
while verify_board(colorMap, board) == False:
apply_single_viable_cell_in_line(colorMap, board)
eliminate_row_column_surrounding_cells_for_each_color(colorMap, board)
eliminate_by_contained_regions(colorMap, board)
if printSteps:
draw_color_map(colorMap, board)
newColorCount = sum(get_color_count(colorMap).values())
if newColorCount == colorCount:
print("No progress made, stopping to avoid infinite loop.")
break
colorCount = newColorCount
if(verify_board(colorMap, board)):
if printSteps:
draw_color_map(colorMap, board)
print("Solved Successfully!")
return True, color_map_to_queens(colorMap)
if printSteps:
draw_color_map(colorMap, board)
return False, 0
def solve_manually(board):
colorMap = get_color_map(board)
# print(colorMap)
# xs = getXs(3,3, testBoard)
# draw(xs, testBoard)
s = get_xs_for_one_color('C2', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C3', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C1', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C4', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C1', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C6', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C5', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C6', colorMap, board)
eliminate_cells(s, colorMap, board)
s = get_xs_for_one_color('C2', colorMap, board)
eliminate_cells(s, colorMap, board)
draw_color_map(colorMap, board)
def main():
# board = GameofCrownsBoardFetcher.gameofcrowns_get_board(8)#, rotate=True)
# solve(board, printSteps=True)
solve(testBoard2, printSteps=True)
# solveManually(testBoard)
if __name__=="__main__":
main()