-
Notifications
You must be signed in to change notification settings - Fork 8
/
mines.py
810 lines (641 loc) · 26.8 KB
/
mines.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# Copyright (C) 2011 by Vincent Povirk
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import collections
import itertools
import sys
if sys.platform == 'cli':
import System
CPU_COUNT = System.Environment.ProcessorCount
else:
#try:
# import multiprocessing
# CPU_COUNT = multiprocessing.cpu_count()
#except ImportError:
# CPU_COUNT = 1
# IronPython seems to be the only common Python implementation that doesn't
# have a GIL and therefore the only implementation that benefits from this.
# Therefore, don't bother making any threads on other implementations.
CPU_COUNT = 1
try:
import thread
import threading
except ImportError:
import dummy_threading as threading
import dummy_thread as thread
CPU_COUNT = 1
class exception(Exception):
pass
class UnsolveableException(exception):
pass
Information = collections.namedtuple('Information', ('spaces', 'count'))
def choose(n, k):
# by Andrew Dalke.
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
global_clusters_checked = set()
global_clusters_hits = itertools.count(0)
global_clusters_misses = itertools.count(0)
global_clusters_solves = itertools.count(0)
global_cluster_probabilities = {}
global_probabilities_hits = itertools.count(0)
global_probabilities_misses = itertools.count(0)
# threading utilities that should probably be elsewhere:
class Promise(object):
def __init__(self, queue):
self.lock = threading.Lock()
self.finished = False
self.value = None
self.queue = queue
self.lock.acquire()
def set(self, value):
self.value = value
self.finished = True
self.lock.release()
def get(self):
if not self.finished:
while not self.lock.acquire(False):
if not self.queue.run_one(False):
self.lock.acquire()
break
self.lock.release()
return self.value
class TaskQueue(object):
def __init__(self, number_of_threads):
self.tasks = []
self.task_sem = threading.Semaphore(0)
for i in range(number_of_threads):
new_thread = threading.Thread(target=TaskQueue.run_forever, args=(self,))
new_thread.daemon = True
new_thread.start()
self.number_of_threads = number_of_threads
def run_task(self, task):
f, args, kwargs, promise = task
try:
promise.set(f(*args, **kwargs))
except BaseException, e:
promise.set(e)
def run_one(self, block=True):
if self.task_sem.acquire(block):
task = self.tasks.pop(0)
self.run_task(task)
return True
else:
return False
def run_forever(self):
while True:
self.run_one()
def add_task(self, f, args=(), kwargs={}, block=True):
promise = Promise(self)
self.tasks.append((f, args, kwargs, promise))
self.task_sem.release()
return promise
class DummyTaskQueue(TaskQueue):
def add_task(self, f, args=(), kwargs={}, block=True):
promise = Promise(self)
promise.set(f(*args, **kwargs))
return promise
if CPU_COUNT == 1:
queue = DummyTaskQueue(0)
else:
queue = TaskQueue(CPU_COUNT)
# actual minesweeper code
class Solver(object):
def __init__(self, spaces):
self.spaces = frozenset(spaces)
self.solved_spaces = dict()
self.information = set()
self.informations_for_space = collections.defaultdict(set)
self.spaces_to_add = []
self.informations_to_add = []
def add_information(self, information):
if information.count < 0 or information.count > len(information.spaces):
raise UnsolveableException()
if information.count == 0:
for space in information.spaces:
self.add_known_value(space, 0)
elif information.count == len(information.spaces):
for space in information.spaces:
self.add_known_value(space, 1)
else:
self.informations_to_add.append(information)
def remove_information(self, information):
self.information.remove(information)
for space in information.spaces:
self.informations_for_space[space].remove(information)
def add_known_value(self, space, value):
self.spaces_to_add.append((space, value))
def copy(self):
self.solve(np=False)
result = Solver(self.spaces)
result.solved_spaces = self.solved_spaces.copy()
result.information = self.information.copy()
for key, value in self.informations_for_space.iteritems():
result.informations_for_space[key] = value.copy()
return result
def get_clusters(self):
informations_unassigned = set(self.information)
result = set()
while informations_unassigned:
information = informations_unassigned.pop()
cluster = set((information,))
unchecked_spaces_in_cluster = set(information.spaces)
while unchecked_spaces_in_cluster:
space = unchecked_spaces_in_cluster.pop()
for information in self.informations_for_space[space]:
if information in informations_unassigned:
informations_unassigned.remove(information)
cluster.add(information)
unchecked_spaces_in_cluster.update(information.spaces)
result.add(frozenset(cluster))
return result
@staticmethod
def get_cluster_probabilities(cluster):
if len(cluster) == 1:
cluster_possibilities = {}
for information in cluster:
break
total = choose(len(information.spaces), information.count)
p = total * information.count / len(information.spaces)
for space in information.spaces:
cluster_possibilities[space] = p
return cluster_possibilities, total
result = global_cluster_probabilities.get(cluster)
if result is not None:
next(global_probabilities_hits)
return result
next(global_probabilities_misses)
spaces = set()
for information in cluster:
spaces.update(information.spaces)
base_solver = Solver(spaces)
for information in cluster:
base_solver.add_information(information)
information1 = information
for information in cluster:
if information is not information1 and not information.spaces.isdisjoint(information1.spaces):
spaces = information.spaces.intersection(information1.spaces)
max_mines = min(len(spaces), information.count, information1.count)
break
else:
raise Exception("This shouldn't happen")
total = 0
possibilities = dict((space, 0) for space in base_solver.spaces)
for i in range(max_mines+1):
solver = base_solver.copy()
try:
solver.add_information(Information(spaces, i))
solver_possibilities, solver_total = solver.get_probabilities()
except UnsolveableException:
continue
total += solver_total
for space in solver.spaces:
if space in solver_possibilities:
possibilities[space] += solver_possibilities[space]
elif solver.solved_spaces[space]:
possibilities[space] += solver_total
global_cluster_probabilities[cluster] = possibilities, total
return possibilities, total
def get_probabilities(self):
self.solve(np=False)
clusters = self.get_clusters()
result = {}
denominator = 1
for cluster in clusters:
possibilities, total = Solver.get_cluster_probabilities(cluster)
for space in result:
result[space] *= total
for space in possibilities:
result[space] = possibilities[space] * denominator
denominator *= total
return result, denominator
@staticmethod
def get_cluster_possibility(cluster, rand):
if len(cluster) == 1:
result = {}
for information in cluster:
break
count = information.count
remaining_spaces = len(information.spaces)
for space in information.spaces:
if rand.randint(1, remaining_spaces) <= count:
count -= 1
result[space] = 1
else:
result[space] = 0
remaining_spaces -= 1
return result
spaces = set()
for information in cluster:
spaces.update(information.spaces)
base_solver = Solver(spaces)
for information in cluster:
base_solver.add_information(information)
information1 = information
# try to choose the same set of spaces as get_cluster_probabilities,
# so we can benefit from caching
for information in cluster:
if information is not information1 and not information.spaces.isdisjoint(information1.spaces):
spaces = information.spaces.intersection(information1.spaces)
max_mines = min(len(spaces), information.count, information1.count)
break
else:
raise Exception("This shouldn't happen")
total = 0
possibilities = [0] * (max_mines+1)
solvers = [None] * (max_mines+1)
for i in range(max_mines+1):
solver = base_solver.copy()
try:
solver.add_information(Information(spaces, i))
_solver_possibilities, possibilities[i] = solver.get_probabilities()
solvers[i] = solver
except UnsolveableException:
possibilities.append
continue
total += possibilities[i]
n = rand.randint(1, total)
for i in range(max_mines+1):
n -= possibilities[i]
if n <= 0:
break
return solvers[i].get_possibility()
def get_possibility(self, rand=None):
self.solve(np=False)
clusters = self.get_clusters()
result = self.solved_spaces.copy()
if rand is None:
import random
rand = random.Random()
rand.seed()
for cluster in clusters:
result.update(Solver.get_cluster_possibility(cluster, rand))
return result
@staticmethod
def solver_from_cluster(cluster):
spaces = set()
for information in cluster:
spaces.update(information.spaces)
result = Solver(spaces)
for information in cluster:
result.information.add(information)
for space in information.spaces:
result.informations_for_space[space].add(information)
return result
def check_state(solver, states_to_validate):
try:
solver.solve(np=False)
except UnsolveableException:
return False
if len(solver.solved_spaces) != len(solver.spaces):
clusters = solver.get_clusters()
states_validated = set(solver.solved_spaces.iteritems())
for cluster in clusters:
cluster_solver = Solver.solver_from_cluster(cluster)
if len(cluster) <= 2:
# solver.solve can handle this trivially
states_validated.update((space, 0) for space in cluster_solver.spaces)
states_validated.update((space, 1) for space in cluster_solver.spaces)
continue
# Find a space in the most informations
max_space = None
max_information = 0
max_information_size = 0
for space in cluster_solver.spaces:
if len(cluster_solver.informations_for_space[space]) > 1:
i = iter(cluster_solver.informations_for_space[space])
information1 = next(i)
information2 = next(i)
spaces = information1.spaces.intersection(information2.spaces)
max_mines = min(len(spaces), information1.count, information2.count)
break
else:
assert False
first_attempt = 0
for space in spaces:
if (space, 0) not in states_to_validate and (space, 1) in states_to_validate:
first_attempt += 1
if first_attempt > max_mines:
first_attempt = max_mines
for i in range(max_mines+1):
if i == 0:
i = first_attempt
elif i == first_attempt:
i = 0
check_solver = cluster_solver.copy()
try:
check_solver.add_information(Information(spaces, i))
except UnsolveableException:
continue
res = check_solver.check_state(states_to_validate)
if res:
break
else:
return False
states_validated.update(res)
return states_validated
else:
return solver.solved_spaces.iteritems()
def solve_cluster(self, cluster):
base_solver = Solver.solver_from_cluster(cluster)
spaces = base_solver.spaces
states_to_validate = set()
states_to_validate.update((x, 0) for x in spaces)
states_to_validate.update((x, 1) for x in spaces)
while states_to_validate:
space, value = states_to_validate.pop()
solver = base_solver.copy()
solver.add_known_value(space, value)
res = solver.check_state(states_to_validate)
if res:
states_validated = res
states_to_validate.difference_update(states_validated)
else:
self.add_known_value(space, value ^ 1)
next(global_clusters_solves)
return True
global_clusters_checked.add(cluster)
next(global_clusters_misses)
return False
def solve_np(self):
clusters = self.get_clusters()
promises = []
res = False
for cluster in clusters:
if len(cluster) <= 2:
continue
if cluster in global_clusters_checked:
next(global_clusters_hits)
continue
promises.append(queue.add_task(Solver.solve_cluster, args=(self, cluster)))
for promise in promises:
if promise.get():
res = True
return res
def solve(self, np=True):
while True:
if self.spaces_to_add:
space, value = self.spaces_to_add.pop()
if space in self.solved_spaces:
if self.solved_spaces[space] != value:
raise UnsolveableException
continue
for information in list(self.informations_for_space.get(space, ())):
new_information = Information(
information.spaces.difference((space,)),
information.count - value)
self.remove_information(information)
self.add_information(new_information)
self.solved_spaces[space] = value
elif self.informations_to_add:
information = self.informations_to_add.pop()
modified = False
for space in information.spaces:
if space in self.solved_spaces:
information = Information(
information.spaces.difference((space,)),
information.count - self.solved_spaces[space])
modified = True
if modified:
self.add_information(information)
continue
if information in self.information:
continue
intersecting_informations = set()
for space in information.spaces:
intersecting_informations.update(self.informations_for_space.get(space, ()))
for other_information in intersecting_informations:
if information.spaces.issubset(other_information.spaces):
new_information = Information(
other_information.spaces.difference(information.spaces),
other_information.count - information.count)
self.remove_information(other_information)
self.add_information(new_information)
elif other_information.spaces.issubset(information.spaces):
new_information = Information(
information.spaces.difference(other_information.spaces),
information.count - other_information.count)
self.add_information(new_information)
break
elif other_information.count - len(other_information.spaces.difference(information.spaces)) >= information.count:
for space in other_information.spaces.difference(information.spaces):
self.add_known_value(space, 1)
for space in information.spaces.difference(other_information.spaces):
self.add_known_value(space, 0)
elif information.count - len(information.spaces.difference(other_information.spaces)) >= other_information.count:
for space in other_information.spaces.difference(information.spaces):
self.add_known_value(space, 0)
for space in information.spaces.difference(other_information.spaces):
self.add_known_value(space, 1)
else:
self.information.add(information)
for space in information.spaces:
self.informations_for_space[space].add(information)
elif not np or not self.solve_np():
break
def picma_main(width, height):
spaces = set((x,y) for x in range(width) for y in range(height))
solver = Solver(spaces)
for y in range(height):
for x in range(width):
char = sys.stdin.read(1)
while char not in '-0123456789':
char = sys.stdin.read(1)
if char.isdigit():
info_count = int(char)
info_spaces = frozenset((xs,ys) for xs in range(x-1, x+2) for ys in range(y-1, y+2)).intersection(spaces)
solver.add_information(Information(info_spaces, info_count))
try:
solver.solve()
except UnsolveableException:
print "This configuration has no solutions."
return
for y in range(height):
for x in range(width):
sys.stdout.write(str(solver.solved_spaces.get((x, y), '-')))
sys.stdout.write('\n')
for i in solver.information:
print i
def mines_main(width, height, total):
spaces = set((x,y) for x in range(width) for y in range(height))
solver = Solver(spaces)
for y in range(height):
for x in range(width):
char = sys.stdin.read(1)
while char not in '-0123456789m':
char = sys.stdin.read(1)
if char.isdigit():
info_count = int(char)
info_spaces = frozenset((xs,ys) for xs in range(x-1, x+2) for ys in range(y-1, y+2)).intersection(spaces)
solver.add_information(Information(info_spaces, info_count))
solver.add_known_value((x, y), 0)
elif char == 'm':
solver.add_known_value((x, y), 1)
solver.add_information(Information(frozenset(spaces), total))
try:
solver.solve()
except UnsolveableException:
print "This configuration has no solutions."
return
sys.stdout.write('\n')
for y in range(height):
for x in range(width):
sys.stdout.write(str(solver.solved_spaces.get((x, y), '-')))
sys.stdout.write('\n')
for i in solver.information:
print i
probabilities, total = solver.get_probabilities()
probabilities = [(probability, space) for (space, probability) in probabilities.iteritems()]
probabilities.sort()
print 'total possible arrangements:', total
total = float(total)
for probability, space in probabilities:
print space, probability / total
class MineMap(object):
def __init__(self, spaces):
self.spaces = frozenset(spaces)
def __getitem__(self, key):
raise NotImplementedError()
def __setitem__(self, key, value):
raise NotImplementedError()
def get_bordering_spaces(self, space):
raise NotImplementedError()
def randomize_p(self, random, p=0.5):
for space in self.spaces:
self[space] = 1 if random.random() < p else 0
def randomize_count(self, random, count):
mines_remaining = count
spaces_remaining = len(self.spaces)
for space in self.spaces:
value = 1 if random.randint(1, spaces_remaining) < mines_remaining else 0
self[space] = value
mines_remaining -= value
spaces_remaining -= 1
class RectMap(MineMap):
def __init__(self, width, height):
spaces = set()
for x in range(width):
for y in range(height):
spaces.add((x, y))
MineMap.__init__(self, spaces)
self.width = width
self.height = height
self.values = [0] * width * height
def __getitem__(self, key):
x, y = key
return self.values[x + y * self.width]
def __setitem__(self, key, value):
x, y = key
self.values[x + y * self.width] = value
def get_bordering_spaces(self, space):
result = set()
x, y = space
for xb in range(max(x-1, 0), min(x+2, self.width)):
for yb in range(max(y-1, 0), min(y+2, self.height)):
result.add((xb, yb))
return result
class PicmaPuzzle(object):
def __init__(self, minemap):
self.minemap = minemap
self.known_spaces = dict()
def create_solver(self):
result = Solver(self.minemap.spaces)
for key, value in self.known_spaces.iteritems():
result.add_information(Information(frozenset(self.minemap.get_bordering_spaces(key)), value))
return result
def make_solveable(self, random):
solver = self.create_solver()
solver.solve()
spaces_left_to_add = set(self.minemap.spaces)
spaces_left_to_add.difference_update(self.known_spaces.iterkeys())
spaces_left_to_add = list(spaces_left_to_add)
random.shuffle(spaces_left_to_add)
while len(self.minemap.spaces) != len(solver.solved_spaces):
if not spaces_left_to_add:
raise ValueError("Unsolveable configuration")
space = spaces_left_to_add.pop()
bordering_spaces = frozenset(self.minemap.get_bordering_spaces(space))
value = sum(self.minemap[s] for s in bordering_spaces)
new_solver = solver.copy()
new_solver.add_information(Information(bordering_spaces, value))
new_solver.solve()
if new_solver.solved_spaces != solver.solved_spaces or \
new_solver.information != solver.information:
self.known_spaces[space] = value
solver = new_solver
def trim(self):
for space, value in self.known_spaces.items():
del self.known_spaces[space]
solver = self.create_solver()
solver.solve()
if len(self.minemap.spaces) != len(solver.solved_spaces):
self.known_spaces[space] = value
def picmagen(rectmap, random):
puzzle = PicmaPuzzle(rectmap)
try:
puzzle.make_solveable(random)
except ValueError:
print "unsolveable configuration:"
for y in range(rectmap.height):
for x in range(rectmap.width):
sys.stdout.write(str(rectmap[x, y]))
sys.stdout.write('\n')
else:
puzzle.trim()
for y in range(rectmap.height):
for x in range(rectmap.width):
sys.stdout.write(str(puzzle.known_spaces.get((x, y), '-')))
sys.stdout.write('\n')
print "hits: ", next(global_clusters_hits)
print "misses: ", next(global_clusters_misses)
print "solves: ", next(global_clusters_solves)
def picmagen_main(width, height):
import random
random = random.SystemRandom()
rectmap = RectMap(width, height)
rectmap.randomize_p(random)
picmagen(rectmap, random)
def picmapregen_main(width, height):
import random
random = random.SystemRandom()
rectmap = RectMap(width, height)
for y in range(height):
for x in range(width):
char = sys.stdin.read(1)
while char not in '01':
char = sys.stdin.read(1)
rectmap[x, y] = int(char)
picmagen(rectmap, random)
if __name__ == '__main__':
if sys.argv[1] == 'picma':
picma_main(int(sys.argv[2]), int(sys.argv[3]))
elif sys.argv[1] == 'mines':
mines_main(int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]))
elif sys.argv[1] == 'picmagen':
picmagen_main(int(sys.argv[2]), int(sys.argv[3]))
elif sys.argv[1] == 'picmapregen':
picmapregen_main(int(sys.argv[2]), int(sys.argv[3]))