multi-puzzle-solver 0.9.6__py3-none-any.whl → 0.9.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multi-puzzle-solver
3
- Version: 0.9.6
3
+ Version: 0.9.7
4
4
  Summary: Efficient solvers for numerous popular and esoteric logic puzzles using CP-SAT
5
5
  Author: Ar-Kareem
6
6
  Project-URL: Homepage, https://github.com/Ar-Kareem/puzzle_solver
@@ -248,6 +248,13 @@ These are all the puzzles that are implemented in this repo. <br> Click on any o
248
248
  </a>
249
249
  </td>
250
250
  </tr>
251
+ <tr>
252
+ <td align="center">
253
+ <a href="#star-battle-puzzle-type-31"><b>Star Battle</b><br><br>
254
+ <img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/star_battle_unsolved.png" alt="Star Battle" width="140">
255
+ </a>
256
+ </td>
257
+ </tr>
251
258
  </table>
252
259
 
253
260
  </div>
@@ -292,6 +299,7 @@ These are all the puzzles that are implemented in this repo. <br> Click on any o
292
299
  - [Stitches (Puzzle Type #28)](#stitches-puzzle-type-28)
293
300
  - [Battleships (Puzzle Type #29)](#battleships-puzzle-type-29)
294
301
  - [Kakurasu (Puzzle Type #30)](#kakurasu-puzzle-type-30)
302
+ - [Star Battle (Puzzle Type #31)](#star-battle-puzzle-type-31)
295
303
  - [Why SAT / CP-SAT?](#why-sat--cp-sat)
296
304
  - [Testing](#testing)
297
305
  - [Contributing](#contributing)
@@ -2506,6 +2514,104 @@ Time taken: 0.00 seconds
2506
2514
 
2507
2515
  ---
2508
2516
 
2517
+ ## Star Battle (Puzzle Type #31)
2518
+
2519
+ * [**Play online**](https://www.puzzle-star-battle.com/)
2520
+
2521
+ * [**Solver Code**][31]
2522
+
2523
+ <details>
2524
+ <summary><strong>Rules</strong></summary>
2525
+
2526
+ You have to place stars on the grid according to the rules:
2527
+ - 2 stars cannot be adjacent horizontally, vertically or diagonally.
2528
+ - For 1★ puzzles, you have to place 1 star on each row, column and shape.
2529
+ - For 2★ puzzles, the stars per row, column and shape must be 2 etc.
2530
+
2531
+ </details>
2532
+
2533
+ **Unsolved puzzle**
2534
+
2535
+ <img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/star_battle_unsolved.png" alt="Star Battle unsolved" width="500">
2536
+
2537
+ Code to utilize this package and solve the puzzle:
2538
+
2539
+ Note that as usual the board is an id of the shape (id is meaningless, just used to identify one shape), and the star_count parameter depenends on the puzzle type.
2540
+
2541
+ ```python
2542
+ from puzzle_solver import star_battle_solver as solver
2543
+ board = np.array([
2544
+ ['00', '00', '00', '00', '00', '01', '01', '01', '01', '01', '01', '01', '01', '01', '02', '02', '02', '03', '03', '03', '03', '03', '03', '03', '03'],
2545
+ ['00', '01', '00', '01', '01', '01', '01', '01', '01', '01', '04', '04', '01', '02', '02', '02', '02', '05', '05', '05', '05', '05', '05', '03', '03'],
2546
+ ['00', '01', '01', '01', '01', '01', '01', '01', '01', '04', '04', '04', '04', '04', '02', '02', '05', '05', '05', '05', '05', '05', '03', '03', '03'],
2547
+ ['00', '01', '06', '04', '04', '04', '04', '04', '04', '04', '04', '04', '04', '04', '02', '05', '05', '05', '05', '05', '05', '05', '03', '07', '03'],
2548
+ ['00', '01', '06', '06', '06', '06', '06', '06', '06', '04', '04', '04', '04', '02', '02', '02', '02', '02', '05', '05', '05', '05', '05', '07', '03'],
2549
+ ['00', '00', '08', '06', '09', '09', '09', '09', '06', '04', '04', '04', '04', '02', '02', '02', '02', '02', '05', '05', '05', '05', '07', '07', '07'],
2550
+ ['00', '08', '08', '08', '08', '09', '09', '06', '06', '06', '04', '04', '04', '04', '02', '02', '02', '05', '05', '05', '07', '07', '07', '07', '07'],
2551
+ ['00', '00', '08', '08', '08', '09', '09', '09', '09', '06', '10', '10', '10', '10', '02', '02', '02', '05', '11', '11', '11', '11', '07', '07', '07'],
2552
+ ['08', '08', '08', '08', '09', '09', '09', '09', '09', '09', '10', '10', '10', '02', '02', '02', '02', '11', '11', '11', '11', '11', '11', '07', '11'],
2553
+ ['08', '08', '08', '08', '09', '09', '09', '09', '09', '10', '10', '10', '10', '02', '02', '02', '11', '11', '11', '11', '11', '11', '11', '07', '11'],
2554
+ ['08', '08', '08', '09', '09', '09', '09', '09', '10', '10', '10', '10', '10', '12', '12', '12', '12', '11', '11', '11', '11', '11', '11', '11', '11'],
2555
+ ['08', '08', '09', '09', '09', '09', '09', '08', '10', '10', '10', '10', '10', '10', '10', '10', '12', '11', '11', '11', '11', '13', '11', '13', '11'],
2556
+ ['14', '08', '08', '08', '08', '08', '08', '08', '10', '10', '10', '10', '10', '12', '12', '12', '12', '12', '11', '11', '11', '13', '11', '13', '15'],
2557
+ ['14', '14', '14', '14', '16', '08', '16', '16', '17', '10', '10', '10', '10', '10', '10', '10', '10', '12', '13', '13', '13', '13', '13', '13', '15'],
2558
+ ['14', '14', '14', '14', '16', '16', '16', '16', '17', '10', '10', '18', '18', '10', '19', '10', '12', '12', '13', '15', '15', '15', '15', '15', '15'],
2559
+ ['14', '14', '14', '14', '14', '16', '16', '17', '17', '18', '18', '18', '19', '19', '19', '10', '10', '10', '13', '15', '15', '15', '15', '15', '15'],
2560
+ ['14', '14', '14', '16', '16', '16', '16', '17', '18', '18', '20', '20', '19', '21', '19', '19', '19', '19', '13', '15', '15', '15', '15', '15', '15'],
2561
+ ['14', '16', '16', '16', '16', '16', '16', '17', '18', '18', '20', '21', '21', '21', '21', '19', '21', '19', '15', '15', '21', '15', '15', '15', '15'],
2562
+ ['14', '14', '14', '16', '16', '17', '17', '17', '18', '20', '20', '21', '20', '21', '21', '19', '21', '19', '15', '21', '21', '15', '15', '15', '15'],
2563
+ ['14', '14', '14', '16', '16', '16', '17', '17', '18', '18', '20', '20', '20', '20', '21', '21', '21', '21', '21', '21', '15', '15', '22', '22', '15'],
2564
+ ['14', '14', '14', '14', '23', '16', '17', '20', '18', '20', '20', '20', '20', '20', '20', '21', '24', '24', '24', '21', '15', '15', '22', '15', '15'],
2565
+ ['14', '14', '14', '14', '23', '20', '17', '20', '18', '20', '20', '20', '20', '24', '24', '24', '24', '24', '24', '21', '15', '22', '22', '22', '15'],
2566
+ ['14', '23', '23', '14', '23', '20', '20', '20', '18', '20', '20', '20', '20', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '22', '15'],
2567
+ ['14', '23', '14', '14', '23', '20', '23', '20', '18', '20', '20', '20', '20', '24', '24', '24', '24', '24', '24', '24', '22', '22', '22', '22', '22'],
2568
+ ['14', '23', '23', '23', '23', '23', '23', '20', '20', '20', '20', '20', '20', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24']
2569
+ ])
2570
+ binst = solver.Board(board=board, star_count=6)
2571
+ solutions = binst.solve_and_print()
2572
+ ```
2573
+
2574
+
2575
+ **Script Output**
2576
+
2577
+ ```python
2578
+ Solution found
2579
+ [' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' '],
2580
+ ['*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' '],
2581
+ [' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' '],
2582
+ ['*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*'],
2583
+ [' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' '],
2584
+ [' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*'],
2585
+ [' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' '],
2586
+ [' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*'],
2587
+ [' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' '],
2588
+ [' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' '],
2589
+ [' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' '],
2590
+ ['*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' '],
2591
+ [' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' '],
2592
+ ['*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' '],
2593
+ [' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' '],
2594
+ [' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*'],
2595
+ [' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' '],
2596
+ [' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*'],
2597
+ [' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
2598
+ ['*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' '],
2599
+ [' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
2600
+ ['*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' '],
2601
+ [' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
2602
+ [' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*'],
2603
+ [' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', '*', ' ', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
2604
+ Solutions found: 1
2605
+ status: OPTIMAL
2606
+ Time taken: 0.38 seconds
2607
+ ```
2608
+
2609
+ **Solved puzzle**
2610
+
2611
+ <img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/star_battle_solved.png" alt="Star Battle solved" width="500">
2612
+
2613
+ ---
2614
+
2509
2615
  ---
2510
2616
 
2511
2617
  ## Why SAT / CP-SAT?
@@ -2587,3 +2693,4 @@ Issues and PRs welcome!
2587
2693
  [28]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/stitches "puzzle_solver/src/puzzle_solver/puzzles/stitches at master · Ar-Kareem/puzzle_solver · GitHub"
2588
2694
  [29]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/battleships "puzzle_solver/src/puzzle_solver/puzzles/battleships at master · Ar-Kareem/puzzle_solver · GitHub"
2589
2695
  [30]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/kakurasu "puzzle_solver/src/puzzle_solver/puzzles/kakurasu at master · Ar-Kareem/puzzle_solver · GitHub"
2696
+ [31]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/star_battle "puzzle_solver/src/puzzle_solver/puzzles/star_battle at master · Ar-Kareem/puzzle_solver · GitHub"
@@ -1,4 +1,4 @@
1
- puzzle_solver/__init__.py,sha256=rqNCaaPZIpdL2Y_Ib96zY8u9Qs_MJaRI3CpJz_vV4_I,2189
1
+ puzzle_solver/__init__.py,sha256=u2RjFlPKYXFnsdHoh7Lo_0Gm99QAv_qv03fOF64Dsag,2269
2
2
  puzzle_solver/core/utils.py,sha256=3LlBDuie_G0uSlzibpQS2ULmEYSZmpJXh1kawj7rjkg,3396
3
3
  puzzle_solver/core/utils_ortools.py,sha256=qLTIzmITqmgGZvg8XpYAZ4c-lhD5sEDQfS8ECdQ_dkM,3005
4
4
  puzzle_solver/puzzles/aquarium/aquarium.py,sha256=BUfkAS2d9eG3TdMoe1cOGGeNYgKUebRvn-z9nsC9gvE,5708
@@ -19,15 +19,16 @@ puzzle_solver/puzzles/keen/keen.py,sha256=tDb6C5S3Q0JAKPsdw-84WQ6PxRADELZHr_BK8F
19
19
  puzzle_solver/puzzles/light_up/light_up.py,sha256=iSA1rjZMFsnI0V0Nxivxox4qZkB7PvUrROSHXcoUXds,4541
20
20
  puzzle_solver/puzzles/magnets/magnets.py,sha256=-Wl49JD_PKeq735zQVMQ3XSQX6gdHiY-7PKw-Sh16jw,6474
21
21
  puzzle_solver/puzzles/map/map.py,sha256=sxc57tapB8Tsgam-yoDitln1o-EB_SbIYvO6WEYy3us,2582
22
- puzzle_solver/puzzles/minesweeper/minesweeper.py,sha256=TK2RSarbBpdhEmVjFydgvGPkLkjBM-a_JiOOmAA1OaU,5219
22
+ puzzle_solver/puzzles/minesweeper/minesweeper.py,sha256=LiQVOGkWCsc1WtX8CdPgL_WwAcaeUFuoi5_eqH8U2Og,5876
23
23
  puzzle_solver/puzzles/mosaic/mosaic.py,sha256=QX_nVpVKQg8OfaUcqFk9tKqsDyVqvZc6-XWvfI3YcSw,2175
24
24
  puzzle_solver/puzzles/nonograms/nonograms.py,sha256=1jmDTOCnmivmBlwtMDyyk3TVqH5IjapzLn7zLQ4qubk,6056
25
25
  puzzle_solver/puzzles/pearl/pearl.py,sha256=AP0whWwwZ-1zKingW14OwseYylNAr6NkXSrvdnPU6Rw,8566
26
26
  puzzle_solver/puzzles/range/range.py,sha256=g6ZuHuulYLpNFsqbnPoIB5KoGPllYppU10-Zzqfj5f8,6993
27
27
  puzzle_solver/puzzles/signpost/signpost.py,sha256=-0_S6ycwzwlUf9-ZhP127Rgo5gMBOHiTM6t08dLLDac,3869
28
28
  puzzle_solver/puzzles/singles/singles.py,sha256=kwMENfqQ-OP3YIz5baY6LRcvYCsNfhImEXN00lwazKM,5658
29
+ puzzle_solver/puzzles/star_battle/star_battle.py,sha256=mNkxV68ejdjaGIZPC2LymfHk1i4T9XuAq1xUG7nnhMw,3192
29
30
  puzzle_solver/puzzles/stitches/stitches.py,sha256=iK8t02q43gH3FPbuIDn4dK0sbaOgZOnw8yHNRNvNuIU,6534
30
- puzzle_solver/puzzles/stitches/parse_map/parse_map.py,sha256=pUB9tG__u4Fq_RCFG_4NNq3LRKAa6FRrvuuL64cUIRM,8866
31
+ puzzle_solver/puzzles/stitches/parse_map/parse_map.py,sha256=kbxTtJlfmE1IEA8Km3rALsfKXFl-0io1vd3d0Z-0OVo,8984
31
32
  puzzle_solver/puzzles/sudoku/sudoku.py,sha256=M_pry7XyKKzlfCF5rFi02lyOrj5GWZzXnDAxmD3NXvI,3588
32
33
  puzzle_solver/puzzles/tents/tents.py,sha256=iyVK2WXfIT5j_9qqlQg0WmwvixwXlZSsHGK3XA-KpII,6283
33
34
  puzzle_solver/puzzles/thermometers/thermometers.py,sha256=nsvJZkm7G8FALT27bpaB0lv5E_AWawqmvapQI8QcYXw,4015
@@ -36,7 +37,7 @@ puzzle_solver/puzzles/tracks/tracks.py,sha256=0K1YZMHiRIMmFwoD_JxB2c_xB6GYV8spgN
36
37
  puzzle_solver/puzzles/undead/undead.py,sha256=IrCUfzQFBem658P5KKqldG7vd2TugTHehcwseCarerM,6604
37
38
  puzzle_solver/puzzles/unruly/unruly.py,sha256=sDF0oKT50G-NshyW2DYrvAgD9q9Ku9ANUyNhGSAu7cQ,3827
38
39
  puzzle_solver/utils/visualizer.py,sha256=2LmNoxEqb9PGWmBmHW6jh6OqFgNYguavhHB-9Dv5EUw,6113
39
- multi_puzzle_solver-0.9.6.dist-info/METADATA,sha256=tyAlmP1wgR8sOPLM89MHtUBv_99kGGU7_McS9m9mfRY,124310
40
- multi_puzzle_solver-0.9.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
41
- multi_puzzle_solver-0.9.6.dist-info/top_level.txt,sha256=exwVUQa-anK9vYrpKzBPvH8bX43iElWI4VeNiAyBGJY,14
42
- multi_puzzle_solver-0.9.6.dist-info/RECORD,,
40
+ multi_puzzle_solver-0.9.7.dist-info/METADATA,sha256=7fMwUGqOwHaq_0hXWLuYPdPD0AYdwPDOVng23I2AwxI,133331
41
+ multi_puzzle_solver-0.9.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
42
+ multi_puzzle_solver-0.9.7.dist-info/top_level.txt,sha256=exwVUQa-anK9vYrpKzBPvH8bX43iElWI4VeNiAyBGJY,14
43
+ multi_puzzle_solver-0.9.7.dist-info/RECORD,,
puzzle_solver/__init__.py CHANGED
@@ -20,6 +20,7 @@ from puzzle_solver.puzzles.pearl import pearl as pearl_solver
20
20
  from puzzle_solver.puzzles.range import range as range_solver
21
21
  from puzzle_solver.puzzles.signpost import signpost as signpost_solver
22
22
  from puzzle_solver.puzzles.singles import singles as singles_solver
23
+ from puzzle_solver.puzzles.star_battle import star_battle as star_battle_solver
23
24
  from puzzle_solver.puzzles.stitches import stitches as stitches_solver
24
25
  from puzzle_solver.puzzles.sudoku import sudoku as sudoku_solver
25
26
  from puzzle_solver.puzzles.tents import tents as tents_solver
@@ -31,4 +32,4 @@ from puzzle_solver.puzzles.unruly import unruly as unruly_solver
31
32
 
32
33
  from puzzle_solver.puzzles.inertia.parse_map.parse_map import main as inertia_image_parser
33
34
 
34
- __version__ = '0.9.6'
35
+ __version__ = '0.9.7'
@@ -1,5 +1,5 @@
1
1
  import time
2
- from typing import Union
2
+ from typing import Union, Optional
3
3
 
4
4
  import numpy as np
5
5
  from ortools.sat.python import cp_model
@@ -10,7 +10,7 @@ from puzzle_solver.core.utils_ortools import generic_solve_all, SingleSolution
10
10
 
11
11
 
12
12
  class Board:
13
- def __init__(self, board: np.array, mine_count: int):
13
+ def __init__(self, board: np.array, mine_count: Optional[int] = None):
14
14
  assert board.ndim == 2, f'board must be 2d, got {board.ndim}'
15
15
  assert all(isinstance(i.item(), str) and (str(i.item()) in [' ', 'F', 'S', 'M', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) for i in np.nditer(board)), 'board must be either F, S, M, 0-9 or space'
16
16
  self.board = board
@@ -28,7 +28,8 @@ class Board:
28
28
  self.model_vars[pos] = self.model.NewBoolVar(f'{pos}')
29
29
 
30
30
  def add_all_constraints(self):
31
- self.model.Add(lxp.Sum(list(self.model_vars.values())) == self.mine_count)
31
+ if self.mine_count is not None:
32
+ self.model.Add(lxp.Sum(list(self.model_vars.values())) == self.mine_count)
32
33
  for pos in get_all_pos(self.V, self.H):
33
34
  c = get_char(self.board, pos)
34
35
  if c in ['F', ' ']:
@@ -55,7 +56,7 @@ def _is_feasible(board: np.array, pos: Pos = None, value: str = None, mine_count
55
56
  return SingleSolution(assignment={pos: solver.value(var) for pos, var in board.model_vars.items()})
56
57
  return len(generic_solve_all(board, board_to_solution, max_solutions=1, verbose=False)) >= 1
57
58
 
58
- def _is_safe(board: np.array, pos: Pos, mine_count: int) -> Union[bool, None]:
59
+ def _is_safe(board: np.array, pos: Pos, mine_count: Optional[int] = None) -> Union[bool, None]:
59
60
  """Returns a True if the position is safe, False if it is a mine, otherwise None"""
60
61
  safe_feasible = _is_feasible(board, pos, 'S', mine_count=mine_count)
61
62
  mine_feasible = _is_feasible(board, pos, 'M', mine_count=mine_count)
@@ -67,7 +68,7 @@ def _is_safe(board: np.array, pos: Pos, mine_count: int) -> Union[bool, None]:
67
68
  return False
68
69
  raise ValueError(f"Position {pos} has both safe and mine infeasible")
69
70
 
70
- def give_next_guess(board: np.array, mine_count: int, verbose: bool = True):
71
+ def give_next_guess(board: np.array, mine_count: Optional[int] = None, verbose: bool = True):
71
72
  tic = time.time()
72
73
  is_feasible = _is_feasible(board, mine_count=mine_count)
73
74
  if not is_feasible:
@@ -108,4 +109,16 @@ def give_next_guess(board: np.array, mine_count: int, verbose: bool = True):
108
109
  print('-'*10)
109
110
  toc = time.time()
110
111
  print(f"Time taken: {toc - tic:.2f} seconds")
111
- return safe_positions, new_garuneed_mine_positions, wrong_flag_positions
112
+ return safe_positions, new_garuneed_mine_positions, wrong_flag_positions
113
+
114
+ def print_board(board: np.array, safe_positions: set[Pos], new_garuneed_mine_positions: set[Pos], wrong_flag_positions: set[Pos]):
115
+ res = np.full((board.shape[0], board.shape[1]), ' ', dtype=object)
116
+ for pos in get_all_pos(board.shape[0], board.shape[1]):
117
+ if pos in safe_positions:
118
+ set_char(res, pos, 'S')
119
+ elif pos in new_garuneed_mine_positions:
120
+ set_char(res, pos, 'M')
121
+ elif get_char(board, pos) == 'F' and pos not in wrong_flag_positions:
122
+ set_char(res, pos, 'F')
123
+
124
+ print(res)
@@ -0,0 +1,63 @@
1
+ import numpy as np
2
+ from ortools.sat.python import cp_model
3
+
4
+ from puzzle_solver.core.utils import Pos, get_all_pos, get_char, set_char, get_neighbors8, get_row_pos, get_col_pos
5
+ from puzzle_solver.core.utils_ortools import generic_solve_all, SingleSolution
6
+
7
+
8
+ class Board:
9
+ def __init__(self, board: np.array, star_count: int = 1):
10
+ assert board.ndim == 2, f'board must be 2d, got {board.ndim}'
11
+ assert board.shape[0] == board.shape[1], 'board must be square'
12
+ assert star_count >= 1 and isinstance(star_count, int), 'star_count must be an integer greater than or equal to 1'
13
+ assert all((str(c.item()).isdecimal() for c in np.nditer(board))), 'board must contain only digits'
14
+ self.board = board
15
+ self.V = board.shape[0]
16
+ self.H = board.shape[1]
17
+ self.N = self.V * self.H
18
+ self.star_count = star_count
19
+ self.block_numbers = set([int(c.item()) for c in np.nditer(board)])
20
+ self.blocks = {i: [pos for pos in get_all_pos(self.V, self.H) if int(get_char(self.board, pos)) == i] for i in self.block_numbers}
21
+
22
+ self.model = cp_model.CpModel()
23
+ self.model_vars: dict[Pos, cp_model.IntVar] = {}
24
+
25
+ self.create_vars()
26
+ self.add_all_constraints()
27
+
28
+ def create_vars(self):
29
+ for pos in get_all_pos(self.V, self.H):
30
+ self.model_vars[pos] = self.model.NewBoolVar(f'{pos}')
31
+
32
+ def add_all_constraints(self):
33
+ # N stars per row / column / block
34
+ for row in range(self.V):
35
+ self.model.Add(sum(self.model_vars[pos] for pos in get_row_pos(row, H=self.H)) == self.star_count)
36
+ for col in range(self.H):
37
+ self.model.Add(sum(self.model_vars[pos] for pos in get_col_pos(col, V=self.V)) == self.star_count)
38
+ for block_i in self.block_numbers:
39
+ self.model.Add(sum(self.model_vars[pos] for pos in self.blocks[block_i]) == self.star_count)
40
+ # stars cant be adjacent
41
+ for pos in get_all_pos(self.V, self.H):
42
+ for neighbor in get_neighbors8(pos, V=self.V, H=self.H):
43
+ self.model.Add(self.model_vars[neighbor] == 0).OnlyEnforceIf(self.model_vars[pos])
44
+
45
+
46
+ def solve_and_print(self, verbose: bool = True):
47
+ def board_to_solution(board: Board, solver: cp_model.CpSolverSolutionCallback) -> SingleSolution:
48
+ assignment: dict[Pos, int] = {}
49
+ for pos, var in board.model_vars.items():
50
+ assignment[pos] = solver.value(var)
51
+ return SingleSolution(assignment=assignment)
52
+ def callback(single_res: SingleSolution):
53
+ print("Solution found")
54
+ res = np.full((self.V, self.H), ' ', dtype=object)
55
+ for pos in get_all_pos(self.V, self.H):
56
+ c = '*' if single_res.assignment[pos] == 1 else ' '
57
+ set_char(res, pos, c)
58
+ for row in range(self.V):
59
+ print(res[row].tolist(), end='')
60
+ if row != self.V - 1:
61
+ print(',', end='')
62
+ print()
63
+ return generic_solve_all(self, board_to_solution, callback=callback if verbose else None, verbose=verbose)
@@ -207,4 +207,5 @@ if __name__ == '__main__':
207
207
  # to run this script and visualize the output, in the root run:
208
208
  # python .\src\puzzle_solver\puzzles\stitches\parse_map\parse_map.py | python .\src\puzzle_solver\utils\visualizer.py --read_stdin
209
209
  # main(Path(__file__).parent / 'input_output' / 'MTM6OSw4MjEsNDAx.png')
210
- main(Path(__file__).parent / 'input_output' / 'weekly_oct_3rd_2025.png')
210
+ # main(Path(__file__).parent / 'input_output' / 'weekly_oct_3rd_2025.png')
211
+ main(Path(__file__).parent / 'input_output' / 'star_battle_67f73ff90cd8cdb4b3e30f56f5261f4968f5dac940bc6.png')