-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart.py
476 lines (382 loc) · 16.2 KB
/
Start.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import random
def reset(positions):
"""
This is used for setup before starting the search
Moves the shape's position so that the top left square is at (0, 0)
"""
min_x, min_y = min(positions, key=lambda x:x[::-1])
return tuple(sorted((x-min_x, y-min_y) for x, y in positions))
def variation(positions):
"""
This is used for setup before starting the search
Returns unique rotations and reflections of the shape
"""
return list({reset(var) for var in (
positions,
[(-y, x) for x, y in positions], # Anti-clockwise 90
[(-x, -y) for x, y in positions], # 180
[( y, -x) for x, y in positions], # Clockwise 90
#[(-x, y) for x, y in positions], # Mirror vertical
#[(-y, -x) for x, y in positions], # Mirror diagonal
[( x, -y) for x, y in positions], # Mirror horizontal
[(y, x) for x, y in positions], # Mirror Anti-clockwise 90
[(-x, y) for x, y in positions], # Mirror 180
[( -y, -x) for x, y in positions], # Mirror Clockwise 90
)})
'''
shapes = [
(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P1"),#"P"),
(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P2"),#"P"),
(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P3"),#"P"),
(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P4"),#"P"),
(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P5"),#"P"),
(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P6"),#"P"),
#(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P7"),#"P"),
#(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P8"),#"P"),
#(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P9"),#"P"),
#(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P10"),#"P"),
#(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P11"),#"P"),
#(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P12"),#"P"),
(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F1"),#"F"),
(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F2"),#"F"),
(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F3"),#"F"),
(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F4"),#"F"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F5"),#"F"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F6"),#"F"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F7"),#"F"),
(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y1"),#"Y"),
(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y2"),#"Y"),
(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y3"),#"Y"),
(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y4"),#"Y"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y5"),#"Y"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y6"),#"Y"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y7"),#"Y"),
(((0, 0), (0, 1), (0, 2), (0, 3), (1, 3)), "L1"),#"L"),
(((0, 0), (0, 1), (0, 2), (0, 3), (1, 3)), "L2"),#"L"),
(((0, 0), (0, 1), (0, 2), (0, 3), (1, 3)), "L3"),#"L"),
#(((0, 0), (0, 1), (0, 2), (0, 3), (1, 3)), "L4"),#"L"),
(((0, 2), (0, 3), (1, 0), (1, 1), (1, 2)), "N1"),#"N"),
(((0, 2), (0, 3), (1, 0), (1, 1), (1, 2)), "N2"),#"N"),
#(((0, 2), (0, 3), (1, 0), (1, 1), (1, 2)), "N3"),#"N"),
(((0, 0), (1, 0), (1, 1), (1, 2), (2, 0)), "T1"),#"T"),
(((0, 0), (1, 0), (1, 1), (1, 2), (2, 0)), "T2"),#"T"),
#(((0, 0), (1, 0), (1, 1), (1, 2), (2, 0)), "T3"),#"T"),
#(((0, 0), (1, 0), (1, 1), (1, 2), (2, 0)), "T4"),#"T"),
(((0, 0), (0, 1), (1, 1), (2, 0), (2, 1)), "U1"),#"U"),
#(((0, 0), (0, 1), (1, 1), (2, 0), (2, 1)), "U2"),#"U"),
(((0, 0), (0, 1), (0, 2), (1, 2), (2, 2)), "V1"),#"V"),
#(((0, 0), (0, 1), (0, 2), (1, 2), (2, 2)), "V2"),#"V"),
(((0, 0), (0, 1), (1, 1), (1, 2), (2, 2)), "W1"),#"W"),
#(((0, 0), (0, 1), (1, 1), (1, 2), (2, 2)), "W2"),#"W"),
(((0, 0), (1, 0), (1, 1), (1, 2), (2, 2)), "Z1"),#"Z"),
#(((0, 0), (1, 0), (1, 1), (1, 2), (2, 2)), "Z2"),#"Z"),
(((0, 1), (1, 0), (1, 1), (1, 2), (2, 1)), "X1"),#"X"),
#(((0, 1), (1, 0), (1, 1), (1, 2), (2, 1)), "X2"),#"X"),
(((0, 0), (0, 1), (0, 2), (0, 3), (0, 4)), "I1")#"I")
]
random.shuffle(shapes)
'''
BASESHAPES1 = [(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P1")]
BASESHAPES2 = [(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F1"),#18
(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y1")]#25
BASESHAPES3 = [(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P2")]
BASESHAPES4 = [(((0, 0), (0, 1), (0, 2), (0, 3), (1, 3)), "L1"),#29
(((0, 2), (0, 3), (1, 0), (1, 1), (1, 2)), "N1"),#35
(((0, 0), (1, 0), (1, 1), (1, 2), (2, 0)), "T1")]#39
BASESHAPES5 = [(((0, 1), (1, 0), (1, 1), (1, 2), (2, 0)), "F2"),
(((0, 1), (1, 0), (1, 1), (1, 2), (1, 3)), "Y2"),
(((0, 0), (0, 1), (1, 1), (2, 0), (2, 1)), "U1"),#41
(((0, 0), (0, 1), (0, 2), (1, 2), (2, 2)), "V1"),#43
(((0, 0), (0, 1), (1, 1), (1, 2), (2, 2)), "W1"),#45
(((0, 0), (1, 0), (1, 1), (1, 2), (2, 2)), "Z1")]#47
BASESHAPES6 = [(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1)), "P3")]
BASESHAPES7 = [(((0, 1), (1, 0), (1, 1), (1, 2), (2, 1)), "X1"),#49
(((0, 0), (0, 1), (0, 2), (0, 3), (0, 4)), "I1")]#50
random.shuffle(BASESHAPES2)
random.shuffle(BASESHAPES4)
random.shuffle(BASESHAPES5)
random.shuffle(BASESHAPES7)
shapes = BASESHAPES1 + BASESHAPES2 + BASESHAPES3 + BASESHAPES4 + BASESHAPES5 + BASESHAPES6 + BASESHAPES7
from range_key_dict import RangeKeyDict
pieceDict = RangeKeyDict({
(0,22):"P",
(22,36):"F",
(36,50):"Y",
(50,58):"L",
(58,66):"N",
(66,73):"T",
(73,77):"U",
(77,81):"V",
(81,85):"W",
(85,89):"Z",
(89,92):"X",
(92,94):"I",
})
shape_variations = {shape: variation(shape) for shape, name in shapes}
def pprintn(grid, size, transpose=False):
"""
Function to print the grid in a nice format
"""
width, height = size
for x in range(width):
for y in range(height):
if grid[(x, y)] == " ":
grid[(x, y)] = " "
if grid[(x, y)] == "M":
grid[(x, y)] = "MM"
if transpose:
for x in range(width):
print("".join([grid[(x, y)] for y in range(height)]))
else:
for y in range(height):
print("".join([grid[(x, y)] for x in range(width)]))
def pprint(grid, size, transpose=False):
"""
Function to print the grid in a nice format
"""
width, height = size
if transpose:
for x in range(width):
print("".join([grid[(x, y)] for y in range(height)]))
else:
for y in range(height):
print("".join([grid[(x, y)] for x in range(width)]))
def solveSpecShapes(grid, size, available_shapes, spec_shapes, start=0):
"""
Recursive function that yields completed/solved grids
Max recursion depth is width*height//5+1
"""
width, height = size
# Traverse the grid left to right, then top to bottom like reading a book
# Look for next open space (".")
for i in range(start, width*height):
y, x = divmod(i, width)
if grid[(x, y)] == ".":
for shape, name in available_shapes:
# Check each rotation and reflection of shape
for shape_var in spec_shapes[shape]:
if all(grid.get((x+xs, y+ys)) == "." for xs, ys in shape_var):
temp_grid = grid.copy()
temp_shapes = available_shapes.copy()
for xs, ys in shape_var:
temp_grid[(x+xs, y+ys)] = name
temp_shapes.remove((shape, name))
yield from solveSpecShapes(temp_grid, size, temp_shapes, spec_shapes, i+1)
return # No more shapes are found, let previous recursion continue
# yield final grid when all grid values have been checked
yield grid
def solve(grid, size, available_shapes, start=0):
"""
Recursive function that yields completed/solved grids
Max recursion depth is width*height//5+1
"""
width, height = size
random.shuffle(BASESHAPES2)
random.shuffle(BASESHAPES4)
random.shuffle(BASESHAPES5)
random.shuffle(BASESHAPES7)
shapes = BASESHAPES1 + BASESHAPES2 + BASESHAPES3 + BASESHAPES4 + BASESHAPES5 + BASESHAPES6 + BASESHAPES7
shape_variations = {shape: variation(shape) for shape, name in shapes}
# Traverse the grid left to right, then top to bottom like reading a book
# Look for next open space (".")
for i in range(start, width*height):
y, x = divmod(i, width)
if grid[(x, y)] == ".":
for shape, name in available_shapes:
# Check each rotation and reflection of shape
for shape_var in shape_variations[shape]:
if all(grid.get((x+xs, y+ys)) == "." for xs, ys in shape_var):
temp_grid = grid.copy()
temp_shapes = available_shapes.copy()
for xs, ys in shape_var:
temp_grid[(x+xs, y+ys)] = name
temp_shapes.remove((shape, name))
yield from solve(temp_grid, size, temp_shapes, i+1)
return # No more shapes are found, let previous recursion continue
# yield final grid when all grid values have been checked
yield grid
from time import time
import copy
from operator import itemgetter
def find(width, height, holes=[], gimmieSol=0):
"""
Program is faster when width is less than height
if width is greater than height, swap them around
Iterate over solve() for more solutions
"""
t = time()
#print(width, height, *holes)
grid = {(x, y):"." for x in range(width) for y in range(height)}
for hole in holes:
for x, y in holes:
grid[(x, y)] = " "
#pprint(grid, (width, height))
#print()
newgrid = copy.deepcopy(grid)
trim = 1
trimAmount = 2*trim
heightTrimmed = height-trimAmount
widthTrimmed = width-trimAmount
'''
#removes middle (better for stratgy) but can leave blocks
#alone resulting in an unsolved puzzle
for y in range(heightTrimmed):
for x in range(widthTrimmed):
if(x == 0 or x == widthTrimmed-1):
pass
elif(y == 0 or y == heightTrimmed-1):
pass
else:
if(grid[(x-1, y-1)] == " " or grid[(x+1, y-1)] == " " or grid[(x-1, y+1)] == " " or grid[(x+1, y+1)] == " " or grid[(x, y)] == " "):
pass
else:
newgrid[(x+trim, y+trim)] = "T"
#pprint(newgrid, (width, height))
#make sure mutiple of 5 have been removed
count = 0
for y in range(heightTrimmed):
for x in range(widthTrimmed):
if newgrid[(x, y)] == "T":
count +=1
count = count%5
#print(count)
holeCountOnT = []
for y in range(heightTrimmed):
for x in range(widthTrimmed):
if newgrid[(x, y)] == "T":
_count = 0
for i in range(-trimAmount,trimAmount+1):
for j in range(-trimAmount,trimAmount+1):
if (newgrid[(x+i, y+j)]== " "):
_count += 1
if _count > 0:
holeCountOnT.append([x,y,_count])
holeCountOnT = sorted(holeCountOnT, key=itemgetter(2))[::-1]
for item in holeCountOnT:
count -= 1
if count < 0:
break
x, y, _ = item
newgrid[(x, y)] = "."
x = trimAmount
y = trimAmount
while count > 0 :
if newgrid[(x, y)] == "T":
newgrid[(x, y)] = "."
count -= 1
x+=1
if (x == widthTrimmed):
x = trimAmount
y+=1
for y in range(heightTrimmed):
for x in range(widthTrimmed):
if newgrid[(x, y)] == "T":
newgrid[(x, y)] = " "
#pprint(newgrid, (width, height))
'''
grid = copy.deepcopy(newgrid)
#print("DONE2")
#print("DONE5")
temp = 0
for solution in solve(grid, (width, height), shapes):
if gimmieSol != temp:
temp += 1
continue
#pprintn(solution, (width, height))
#print("DONE6")
remove=[]
for y in range(height):
for x in range(width):
temp=""
try:
temp+=solution[(x+1, y)]
temp+=solution[(x-1, y)]
temp+=solution[(x, y+1)]
temp+=solution[(x, y-1)]
if(temp.find(" ") != -1):
pass
else:
remove.append(solution[(x, y)])
except:
pass
VALUETOCOUNT = list( dict.fromkeys(remove) )
actRem = []
for item in VALUETOCOUNT:
if (remove.count(item) == 5):
actRem.append(item)
for y in range(height):
for x in range(width):
for item in actRem:
if(solution[(x, y)] == item):
solution[(x, y)] = " "
pprintn(solution, (width, height))
print(f"{time()-t:.3f}s\n")
return solution
else:
pprint(grid, (width, height))
print("No solution")
print(f"{time()-t:.3f}s\n")
return False
def findSpecShapes(width, height, spec_shapes, findWith, holes=[]):
"""
Program is faster when width is less than height
if width is greater than height, swap them around
Iterate over solve() for more solutions
"""
t = time()
#print(width, height, *holes)
grid = {(x, y):"." for x in range(width) for y in range(height)}
for hole in holes:
for x, y in holes:
grid[(x, y)] = " "
#pprint(grid, (width, height))
#print()
newgrid = copy.deepcopy(grid)
trim = 1
trimAmount = 2*trim
heightTrimmed = height-trimAmount
widthTrimmed = width-trimAmount
grid = copy.deepcopy(newgrid)
for solution in solveSpecShapes(grid, (width, height), shapes, spec_shapes):
found = [False,False,False]
for key in solution:
temp = 0
for contains in findWith:
if solution[key][0] == contains:
found[temp] = True
temp += 1
if any(found) != True:
continue
remove=[]
for y in range(height):
for x in range(width):
temp=""
try:
temp+=solution[(x+1, y)]
temp+=solution[(x-1, y)]
temp+=solution[(x, y+1)]
temp+=solution[(x, y-1)]
if(temp.find(" ") != -1):
pass
else:
remove.append(solution[(x, y)])
except:
pass
VALUETOCOUNT = list( dict.fromkeys(remove) )
actRem = []
for item in VALUETOCOUNT:
if (remove.count(item) == 5):
actRem.append(item)
for y in range(height):
for x in range(width):
for item in actRem:
if(solution[(x, y)] == item):
solution[(x, y)] = " "
pprintn(solution, (width, height))
print(f"{time()-t:.3f}s\n")
return solution
else:
pprint(grid, (width, height))
print("No solution")
print(f"{time()-t:.3f}s\n")
return False