multi-puzzle-solver 0.9.1__py3-none-any.whl → 0.9.3__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.
- {multi_puzzle_solver-0.9.1.dist-info → multi_puzzle_solver-0.9.3.dist-info}/METADATA +196 -26
- {multi_puzzle_solver-0.9.1.dist-info → multi_puzzle_solver-0.9.3.dist-info}/RECORD +8 -5
- puzzle_solver/__init__.py +3 -1
- puzzle_solver/puzzles/aquarium/aquarium.py +112 -0
- puzzle_solver/puzzles/thermometers/thermometers.py +82 -0
- puzzle_solver/utils/visualizer.py +108 -0
- {multi_puzzle_solver-0.9.1.dist-info → multi_puzzle_solver-0.9.3.dist-info}/WHEEL +0 -0
- {multi_puzzle_solver-0.9.1.dist-info → multi_puzzle_solver-0.9.3.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: multi-puzzle-solver
|
3
|
-
Version: 0.9.
|
3
|
+
Version: 0.9.3
|
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
|
@@ -89,135 +89,147 @@ These are all the puzzles that are implemented in this repo. <br> Click on any o
|
|
89
89
|
<tr>
|
90
90
|
<td align="center">
|
91
91
|
<a href="#nonograms-puzzle-type-1"><b>Nonograms</b><br><br>
|
92
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/nonogram_solved.png" alt="Nonograms"
|
92
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/nonogram_solved.png" alt="Nonograms" width="140">
|
93
93
|
</a>
|
94
94
|
</td>
|
95
95
|
<td align="center">
|
96
96
|
<a href="#sudoku-puzzle-type-2"><b>Sudoku</b><br><br>
|
97
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/sudoku_solved.png" alt="Sudoku"
|
97
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/sudoku_solved.png" alt="Sudoku" width="140">
|
98
98
|
</a>
|
99
99
|
</td>
|
100
100
|
<td align="center">
|
101
101
|
<a href="#minesweeper-puzzle-type-3"><b>Minesweeper</b><br><br>
|
102
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/minesweeper_pre.png" alt="Minesweeper"
|
102
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/minesweeper_pre.png" alt="Minesweeper" width="140">
|
103
103
|
</a>
|
104
104
|
</td>
|
105
105
|
<td align="center">
|
106
106
|
<a href="#dominosa-puzzle-type-4"><b>Dominosa</b><br><br>
|
107
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/dominosa_solved.png" alt="Dominosa"
|
107
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/dominosa_solved.png" alt="Dominosa" width="140">
|
108
108
|
</a>
|
109
109
|
</td>
|
110
110
|
<td align="center">
|
111
111
|
<a href="#light-up-puzzle-type-5"><b>Light Up</b><br><br>
|
112
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/lightup_solved.png" alt="Light Up"
|
112
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/lightup_solved.png" alt="Light Up" width="140">
|
113
113
|
</a>
|
114
114
|
</td>
|
115
115
|
</tr>
|
116
116
|
<tr>
|
117
117
|
<td align="center">
|
118
118
|
<a href="#tents-puzzle-type-6"><b>Tents</b><br><br>
|
119
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/tents_solved.png" alt="Tents"
|
119
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/tents_solved.png" alt="Tents" width="140">
|
120
120
|
</a>
|
121
121
|
</td>
|
122
122
|
<td align="center">
|
123
123
|
<a href="#filling-puzzle-type-7"><b>Filling</b><br><br>
|
124
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/filling_solved.png" alt="Filling"
|
124
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/filling_solved.png" alt="Filling" width="140">
|
125
125
|
</a>
|
126
126
|
</td>
|
127
127
|
<td align="center">
|
128
128
|
<a href="#keen-puzzle-type-8"><b>Keen</b><br><br>
|
129
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/keen_solved.png" alt="Keen"
|
129
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/keen_solved.png" alt="Keen" width="140">
|
130
130
|
</a>
|
131
131
|
</td>
|
132
132
|
<td align="center">
|
133
133
|
<a href="#towers-puzzle-type-9"><b>Towers</b><br><br>
|
134
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/towers_solved.png" alt="Towers"
|
134
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/towers_solved.png" alt="Towers" width="140">
|
135
135
|
</a>
|
136
136
|
</td>
|
137
137
|
<td align="center">
|
138
138
|
<a href="#singles-puzzle-type-10"><b>Singles</b><br><br>
|
139
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/singles_solved.png" alt="Singles"
|
139
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/singles_solved.png" alt="Singles" width="140">
|
140
140
|
</a>
|
141
141
|
</td>
|
142
142
|
</tr>
|
143
143
|
<tr>
|
144
144
|
<td align="center">
|
145
145
|
<a href="#magnets-puzzle-type-11"><b>Magnets</b><br><br>
|
146
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/magnets_solved.png" alt="Magnets"
|
146
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/magnets_solved.png" alt="Magnets" width="140">
|
147
147
|
</a>
|
148
148
|
</td>
|
149
149
|
<td align="center">
|
150
150
|
<a href="#signpost-puzzle-type-12"><b>Signpost</b><br><br>
|
151
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/signpost_solved.png" alt="Signpost"
|
151
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/signpost_solved.png" alt="Signpost" width="140">
|
152
152
|
</a>
|
153
153
|
</td>
|
154
154
|
<td align="center">
|
155
155
|
<a href="#range-puzzle-type-13"><b>Range</b><br><br>
|
156
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/range_solved.png" alt="Range"
|
156
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/range_solved.png" alt="Range" width="140">
|
157
157
|
</a>
|
158
158
|
</td>
|
159
159
|
<td align="center">
|
160
160
|
<a href="#undead-puzzle-type-14"><b>Undead</b><br><br>
|
161
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/undead_solved.png" alt="Undead"
|
161
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/undead_solved.png" alt="Undead" width="140">
|
162
162
|
</a>
|
163
163
|
</td>
|
164
164
|
<td align="center">
|
165
165
|
<a href="#unruly-puzzle-type-15"><b>Unruly</b><br><br>
|
166
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/unruly_solved.png" alt="Unruly"
|
166
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/unruly_solved.png" alt="Unruly" width="140">
|
167
167
|
</a>
|
168
168
|
</td>
|
169
169
|
</tr>
|
170
170
|
<tr>
|
171
171
|
<td align="center">
|
172
172
|
<a href="#tracks-puzzle-type-16"><b>Tracks</b><br><br>
|
173
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/tracks_solved.png" alt="Tracks"
|
173
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/tracks_solved.png" alt="Tracks" width="140">
|
174
174
|
</a>
|
175
175
|
</td>
|
176
176
|
<td align="center">
|
177
177
|
<a href="#mosaic-puzzle-type-17"><b>Mosaic</b><br><br>
|
178
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/mosaic_solved.png" alt="Mosaic"
|
178
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/mosaic_solved.png" alt="Mosaic" width="140">
|
179
179
|
</a>
|
180
180
|
</td>
|
181
181
|
<td align="center">
|
182
182
|
<a href="#map-puzzle-type-18"><b>Map</b><br><br>
|
183
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/map_solved.png" alt="Map"
|
183
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/map_solved.png" alt="Map" width="140">
|
184
184
|
</a>
|
185
185
|
</td>
|
186
186
|
<td align="center">
|
187
187
|
<a href="#pearl-puzzle-type-19"><b>Pearl</b><br><br>
|
188
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/pearl_solved.png" alt="Pearl"
|
188
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/pearl_solved.png" alt="Pearl" width="140">
|
189
189
|
</a>
|
190
190
|
</td>
|
191
191
|
<td align="center">
|
192
192
|
<a href="#bridges-puzzle-type-20"><b>Bridges</b><br><br>
|
193
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/bridges_solved.png" alt="Bridges"
|
193
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/bridges_solved.png" alt="Bridges" width="140">
|
194
194
|
</a>
|
195
195
|
</td>
|
196
196
|
</tr>
|
197
197
|
<tr>
|
198
198
|
<td align="center">
|
199
199
|
<a href="#inertia-puzzle-type-21"><b>Inertia</b><br><br>
|
200
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/inertia_unsolved.png" alt="Inertia"
|
200
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/inertia_unsolved.png" alt="Inertia" width="140">
|
201
201
|
</a>
|
202
202
|
</td>
|
203
203
|
<td align="center">
|
204
204
|
<a href="#guess-puzzle-type-22"><b>Guess</b><br><br>
|
205
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/guess_3.png" alt="Guess"
|
205
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/guess_3.png" alt="Guess" width="140">
|
206
206
|
</a>
|
207
207
|
</td>
|
208
208
|
<td align="center">
|
209
209
|
<a href="#chess-range-puzzle-type-23"><b>Chess Range</b><br><br>
|
210
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/chess_range_unsolved.png" alt="Chess range"
|
210
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/chess_range_unsolved.png" alt="Chess range" width="140">
|
211
211
|
</a>
|
212
212
|
</td>
|
213
213
|
<td align="center">
|
214
214
|
<a href="#chess-solo-puzzle-type-24"><b>Chess Solo</b><br><br>
|
215
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/chess_solo_unsolved.png" alt="Chess solo"
|
215
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/chess_solo_unsolved.png" alt="Chess solo" width="140">
|
216
216
|
</a>
|
217
217
|
</td>
|
218
218
|
<td align="center">
|
219
219
|
<a href="#chess-melee-puzzle-type-25"><b>Chess Melee</b><br><br>
|
220
|
-
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/chess_melee_unsolved.png" alt="Chess melee"
|
220
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/chess_melee_unsolved.png" alt="Chess melee" width="140">
|
221
|
+
</a>
|
222
|
+
</td>
|
223
|
+
</tr>
|
224
|
+
<tr>
|
225
|
+
<td align="center">
|
226
|
+
<a href="#thermometers-puzzle-type-26"><b>Thermometers</b><br><br>
|
227
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/thermometers_solved.png" alt="Thermometers" width="140">
|
228
|
+
</a>
|
229
|
+
</td>
|
230
|
+
<td align="center">
|
231
|
+
<a href="#aquarium-puzzle-type-27"><b>Aquarium</b><br><br>
|
232
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/aquarium_solved.png" alt="Aquarium" width="140">
|
221
233
|
</a>
|
222
234
|
</td>
|
223
235
|
</tr>
|
@@ -260,6 +272,8 @@ These are all the puzzles that are implemented in this repo. <br> Click on any o
|
|
260
272
|
- [Chess Range (Puzzle Type #23)](#chess-range-puzzle-type-23)
|
261
273
|
- [Chess Solo (Puzzle Type #24)](#chess-solo-puzzle-type-24)
|
262
274
|
- [Chess Melee (Puzzle Type #25)](#chess-melee-puzzle-type-25)
|
275
|
+
- [Thermometers (Puzzle Type #26)](#thermometers-puzzle-type-26)
|
276
|
+
- [Aquarium (Puzzle Type #27)](#aquarium-puzzle-type-27)
|
263
277
|
- [Why SAT / CP-SAT?](#why-sat--cp-sat)
|
264
278
|
- [Testing](#testing)
|
265
279
|
- [Contributing](#contributing)
|
@@ -2023,6 +2037,7 @@ Code to utilize this package and solve the puzzle:
|
|
2023
2037
|
(Note that this puzzle does not typically have a unique solution. Thus, we specify here that we only want the first valid solution that the solver finds.)
|
2024
2038
|
|
2025
2039
|
```python
|
2040
|
+
from puzzle_solver import chess_solo_solver as solver
|
2026
2041
|
# algebraic notation
|
2027
2042
|
board = ['Kc6', 'Rc5', 'Rc4', 'Pb3', 'Bd3', 'Pd2', 'Pe3', 'Nf2', 'Ng2', 'Qg3', 'Pg6']
|
2028
2043
|
binst = solver.Board(board)
|
@@ -2073,6 +2088,7 @@ Code to utilize this package and solve the puzzle:
|
|
2073
2088
|
(Note that this puzzle does not typically have a unique solution. Thus, we specify here that we only want the first valid solution that the solver finds.)
|
2074
2089
|
|
2075
2090
|
```python
|
2091
|
+
from puzzle_solver import chess_melee_solver as solver
|
2076
2092
|
# algebraic notation
|
2077
2093
|
board = ['Pb7', 'Nc7', 'Bc6', 'Ne6', 'Pb5', 'Rc4', 'Qb3', 'Rf7', 'Rb6', 'Pe5', 'Nc3', 'Pd3', 'Nf3']
|
2078
2094
|
colors = ['B', 'B', 'B', 'B', 'B', 'B', 'B', 'W', 'W', 'W', 'W', 'W', 'W']
|
@@ -2097,6 +2113,158 @@ Time taken: 6.24 seconds
|
|
2097
2113
|
|
2098
2114
|
---
|
2099
2115
|
|
2116
|
+
## Thermometers (Puzzle Type #26)
|
2117
|
+
|
2118
|
+
* [**Play online**](https://www.puzzle-thermometers.com/)
|
2119
|
+
|
2120
|
+
* [**Solver Code**][26]
|
2121
|
+
|
2122
|
+
<details>
|
2123
|
+
<summary><strong>Rules</strong></summary>
|
2124
|
+
|
2125
|
+
You have to fill some thermometers with mercury starting from the bulb and going toward the end without gaps.
|
2126
|
+
|
2127
|
+
The numbers outside the grid show the number of filled cells horizontally and vertically.
|
2128
|
+
|
2129
|
+
</details>
|
2130
|
+
|
2131
|
+
**Unsolved puzzle**
|
2132
|
+
|
2133
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/thermometers_unsolved.png" alt="Thermometers unsolved" width="500">
|
2134
|
+
|
2135
|
+
Code to utilize this package and solve the puzzle:
|
2136
|
+
|
2137
|
+
(Note that this puzzle does not typically have a unique solution. Thus, we specify here that we only want the first valid solution that the solver finds.)
|
2138
|
+
|
2139
|
+
```python
|
2140
|
+
from puzzle_solver import thermometers_solver as solver
|
2141
|
+
board = np.array([
|
2142
|
+
['R', 'R', 'D', 'R', 'D', 'R', 'X', 'D', 'L', 'X', 'L', 'L', 'L', 'L', 'L'],
|
2143
|
+
['D', 'D', 'D', 'U', 'X', 'U', 'X', 'R', 'R', 'R', 'R', 'D', 'D', 'R', 'U'],
|
2144
|
+
['D', 'D', 'D', 'U', 'X', 'U', 'U', 'R', 'R', 'R', 'X', 'D', 'D', 'D', 'D'],
|
2145
|
+
['X', 'D', 'D', 'U', 'U', 'U', 'L', 'U', 'R', 'R', 'D', 'X', 'D', 'X', 'X'],
|
2146
|
+
['X', 'D', 'D', 'U', 'U', 'R', 'R', 'R', 'R', 'X', 'R', 'X', 'D', 'R', 'X'],
|
2147
|
+
['U', 'D', 'D', 'U', 'U', 'R', 'X', 'R', 'R', 'R', 'R', 'D', 'D', 'R', 'D'],
|
2148
|
+
['U', 'D', 'D', 'R', 'R', 'X', 'R', 'R', 'R', 'R', 'D', 'D', 'R', 'X', 'D'],
|
2149
|
+
['U', 'D', 'D', 'U', 'X', 'L', 'X', 'L', 'R', 'X', 'X', 'R', 'X', 'X', 'L'],
|
2150
|
+
['U', 'D', 'D', 'R', 'X', 'U', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L'],
|
2151
|
+
['X', 'D', 'X', 'U', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'D', 'U'],
|
2152
|
+
['U', 'D', 'X', 'U', 'R', 'R', 'X', 'R', 'R', 'R', 'R', 'X', 'X', 'L', 'U'],
|
2153
|
+
['U', 'R', 'U', 'U', 'R', 'X', 'R', 'X', 'R', 'X', 'R', 'R', 'R', 'R', 'U'],
|
2154
|
+
['U', 'R', 'X', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'X', 'X', 'L'],
|
2155
|
+
['U', 'U', 'R', 'R', 'X', 'D', 'R', 'R', 'D', 'R', 'X', 'X', 'L', 'L', 'U'],
|
2156
|
+
['U', 'U', 'U', 'L', 'L', 'R', 'X', 'X', 'L', 'U', 'R', 'R', 'R', 'U', 'U'],
|
2157
|
+
])
|
2158
|
+
top = np.array([7, 4, 12, 8, 4, 6, 5, 7, 5, 4, 8, 9, 13, 8, 12])
|
2159
|
+
side = np.array([8, 10, 9, 10, 6, 10, 4, 6, 6, 10, 5, 7, 6, 6, 9])
|
2160
|
+
binst = solver.Board(board=board, top=top, side=side)
|
2161
|
+
solutions = binst.solve_and_print()
|
2162
|
+
```
|
2163
|
+
**Script Output**
|
2164
|
+
|
2165
|
+
```python
|
2166
|
+
Solution found
|
2167
|
+
[['X' 'X' 'X' ' ' ' ' ' ' ' ' 'X' 'X' ' ' ' ' ' ' 'X' 'X' 'X']
|
2168
|
+
['X' ' ' 'X' ' ' ' ' ' ' ' ' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X']
|
2169
|
+
['X' ' ' 'X' 'X' ' ' 'X' 'X' 'X' ' ' ' ' ' ' 'X' 'X' ' ' 'X']
|
2170
|
+
[' ' ' ' 'X' 'X' ' ' 'X' 'X' 'X' 'X' 'X' 'X' ' ' 'X' ' ' 'X']
|
2171
|
+
[' ' ' ' 'X' 'X' ' ' ' ' ' ' ' ' ' ' ' ' 'X' ' ' 'X' 'X' 'X']
|
2172
|
+
[' ' ' ' 'X' 'X' ' ' ' ' ' ' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X']
|
2173
|
+
[' ' ' ' 'X' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'X' 'X' ' ' 'X']
|
2174
|
+
[' ' ' ' 'X' ' ' ' ' ' ' 'X' 'X' ' ' ' ' ' ' 'X' 'X' ' ' 'X']
|
2175
|
+
[' ' ' ' 'X' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'X' 'X' 'X' 'X' 'X']
|
2176
|
+
[' ' ' ' ' ' ' ' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' ' ' 'X']
|
2177
|
+
[' ' ' ' ' ' 'X' 'X' 'X' 'X' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'X']
|
2178
|
+
['X' ' ' ' ' 'X' ' ' ' ' ' ' ' ' ' ' ' ' 'X' 'X' 'X' 'X' 'X']
|
2179
|
+
['X' 'X' 'X' 'X' 'X' 'X' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
|
2180
|
+
['X' 'X' 'X' ' ' ' ' 'X' ' ' ' ' ' ' ' ' ' ' ' ' 'X' 'X' ' ']
|
2181
|
+
['X' 'X' 'X' 'X' 'X' ' ' ' ' ' ' ' ' ' ' 'X' 'X' 'X' 'X' ' ']]
|
2182
|
+
Solutions found: 1
|
2183
|
+
status: OPTIMAL
|
2184
|
+
Time taken: 0.01 seconds
|
2185
|
+
```
|
2186
|
+
|
2187
|
+
## Aquarium (Puzzle Type #27)
|
2188
|
+
|
2189
|
+
* [**Play online**](https://www.puzzle-aquarium.com/)
|
2190
|
+
|
2191
|
+
* [**Solver Code**][27]
|
2192
|
+
|
2193
|
+
<details>
|
2194
|
+
<summary><strong>Rules</strong></summary>
|
2195
|
+
|
2196
|
+
The puzzle is played on a rectangular grid divided into blocks called "aquariums"
|
2197
|
+
|
2198
|
+
You have to "fill" the aquariums with water up to a certain level or leave it empty.
|
2199
|
+
|
2200
|
+
The water level in each aquarium is one and the same across its full width
|
2201
|
+
|
2202
|
+
The numbers outside the grid show the number of filled cells horizontally and vertically.
|
2203
|
+
|
2204
|
+
</details>
|
2205
|
+
|
2206
|
+
**Unsolved puzzle**
|
2207
|
+
|
2208
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/aquarium_unsolved.png" alt="Aquarium unsolved" width="500">
|
2209
|
+
|
2210
|
+
Code to utilize this package and solve the puzzle:
|
2211
|
+
|
2212
|
+
(Note that this puzzle does not typically have a unique solution. Thus, we specify here that we only want the first valid solution that the solver finds.)
|
2213
|
+
|
2214
|
+
```python
|
2215
|
+
from puzzle_solver import aquarium_solver as solver
|
2216
|
+
board = np.array([
|
2217
|
+
['01', '01', '01', '01', '02', '02', '02', '03', '03', '03', '03', '04', '05', '05', '05'],
|
2218
|
+
['01', '02', '02', '02', '02', '06', '07', '07', '03', '08', '03', '04', '04', '05', '09'],
|
2219
|
+
['01', '01', '02', '11', '06', '06', '06', '12', '12', '08', '13', '13', '13', '09', '09'],
|
2220
|
+
['01', '11', '11', '11', '14', '06', '06', '12', '12', '15', '15', '13', '09', '09', '09'],
|
2221
|
+
['01', '01', '11', '11', '14', '12', '12', '12', '16', '16', '15', '13', '13', '17', '09'],
|
2222
|
+
['45', '11', '11', '14', '14', '12', '42', '42', '42', '15', '15', '13', '13', '17', '18'],
|
2223
|
+
['45', '11', '11', '14', '14', '12', '12', '43', '15', '15', '20', '13', '13', '17', '18'],
|
2224
|
+
['46', '46', '11', '19', '19', '19', '43', '43', '44', '20', '20', '20', '13', '17', '18'],
|
2225
|
+
['46', '22', '23', '23', '23', '19', '43', '21', '21', '24', '24', '24', '25', '17', '17'],
|
2226
|
+
['22', '22', '22', '23', '19', '19', '26', '24', '24', '24', '28', '28', '25', '17', '33'],
|
2227
|
+
['22', '22', '23', '23', '27', '27', '26', '26', '24', '24', '29', '29', '25', '25', '33'],
|
2228
|
+
['22', '22', '35', '27', '27', '26', '26', '26', '26', '30', '30', '30', '25', '34', '34'],
|
2229
|
+
['37', '22', '35', '35', '35', '35', '35', '26', '26', '30', '31', '31', '32', '32', '40'],
|
2230
|
+
['37', '37', '37', '36', '36', '35', '26', '26', '26', '40', '40', '40', '40', '40', '40'],
|
2231
|
+
['37', '37', '37', '37', '35', '35', '38', '38', '39', '39', '40', '40', '40', '41', '41'],
|
2232
|
+
])
|
2233
|
+
top = np.array([6, 6, 5, 3, 3, 4, 7, 6, 9, 6, 3, 4, 9, 6, 7])
|
2234
|
+
side = np.array([3, 5, 1, 2, 5, 3, 10, 10, 5, 3, 7, 3, 7, 8, 12])
|
2235
|
+
binst = solver.Board(board=board, top=top, side=side)
|
2236
|
+
solutions = binst.solve_and_print()
|
2237
|
+
```
|
2238
|
+
**Script Output**
|
2239
|
+
|
2240
|
+
```python
|
2241
|
+
Solution found
|
2242
|
+
[['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1' '1' '1']
|
2243
|
+
['0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '1' '1' '1' '1' '0']
|
2244
|
+
['0' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '0' '0' '0' '0']
|
2245
|
+
['0' '0' '0' '0' '0' '1' '1' '0' '0' '0' '0' '0' '0' '0' '0']
|
2246
|
+
['1' '1' '0' '0' '0' '0' '0' '0' '1' '1' '0' '0' '0' '0' '1']
|
2247
|
+
['0' '0' '0' '0' '0' '0' '1' '1' '1' '0' '0' '0' '0' '0' '0']
|
2248
|
+
['1' '1' '1' '0' '0' '1' '1' '0' '1' '1' '0' '1' '1' '0' '1']
|
2249
|
+
['1' '1' '1' '0' '0' '0' '1' '1' '0' '1' '1' '1' '1' '0' '1']
|
2250
|
+
['1' '0' '0' '0' '0' '0' '1' '1' '1' '0' '0' '0' '1' '0' '0']
|
2251
|
+
['0' '0' '0' '0' '1' '1' '0' '0' '0' '0' '0' '0' '1' '0' '0']
|
2252
|
+
['0' '0' '1' '1' '0' '0' '0' '0' '1' '1' '0' '0' '1' '1' '1']
|
2253
|
+
['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1' '1' '1']
|
2254
|
+
['0' '1' '0' '0' '0' '0' '0' '1' '1' '0' '1' '1' '1' '1' '0']
|
2255
|
+
['1' '1' '1' '1' '1' '0' '1' '1' '1' '0' '0' '0' '0' '0' '0']
|
2256
|
+
['1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '0' '0' '0' '1' '1']]
|
2257
|
+
Solutions found: 1
|
2258
|
+
status: OPTIMAL
|
2259
|
+
Time taken: 0.02 seconds
|
2260
|
+
```
|
2261
|
+
|
2262
|
+
**Solved puzzle**
|
2263
|
+
|
2264
|
+
<img src="https://raw.githubusercontent.com/Ar-Kareem/puzzle_solver/master/images/aquarium_solved.png" alt="Aquarium solved" width="500">
|
2265
|
+
|
2266
|
+
---
|
2267
|
+
|
2100
2268
|
---
|
2101
2269
|
|
2102
2270
|
## Why SAT / CP-SAT?
|
@@ -2173,3 +2341,5 @@ Issues and PRs welcome!
|
|
2173
2341
|
[23]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/chess_range "puzzle_solver/src/puzzle_solver/puzzles/chess_range at master · Ar-Kareem/puzzle_solver · GitHub"
|
2174
2342
|
[24]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/chess_range#chess-solo-puzzle-type-24 "puzzle_solver/src/puzzle_solver/puzzles/chess_range at master · Ar-Kareem/puzzle_solver · GitHub"
|
2175
2343
|
[25]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/chess_range#chess-melee-puzzle-type-25 "puzzle_solver/src/puzzle_solver/puzzles/chess_range at master · Ar-Kareem/puzzle_solver · GitHub"
|
2344
|
+
[26]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/thermometers "puzzle_solver/src/puzzle_solver/puzzles/thermometers at master · Ar-Kareem/puzzle_solver · GitHub"
|
2345
|
+
[27]: https://github.com/Ar-Kareem/puzzle_solver/tree/master/src/puzzle_solver/puzzles/aquarium "puzzle_solver/src/puzzle_solver/puzzles/aquarium at master · Ar-Kareem/puzzle_solver · GitHub"
|
@@ -1,6 +1,7 @@
|
|
1
|
-
puzzle_solver/__init__.py,sha256=
|
1
|
+
puzzle_solver/__init__.py,sha256=sOawTTp0FlKJI45RevxucH2ktl_9MbY5xem8txM8vSk,1967
|
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
|
+
puzzle_solver/puzzles/aquarium/aquarium.py,sha256=BUfkAS2d9eG3TdMoe1cOGGeNYgKUebRvn-z9nsC9gvE,5708
|
4
5
|
puzzle_solver/puzzles/bridges/bridges.py,sha256=zUT0TMIu8l982fqDMJfsTnTgqm48nG0iH8flsGT45_E,5489
|
5
6
|
puzzle_solver/puzzles/chess_range/chess_melee.py,sha256=KnfD_Sxd8bso46eQYpIemp4MIqOUNoonyRVe6soK8kc,231
|
6
7
|
puzzle_solver/puzzles/chess_range/chess_range.py,sha256=IaldwJR4d0VAUxME2QyvtJdUNzGzDV0FGs1iq9KqsRU,21072
|
@@ -25,11 +26,13 @@ puzzle_solver/puzzles/signpost/signpost.py,sha256=D19ua8rVwO6sgXq4nVLkZfEyz8hqc6
|
|
25
26
|
puzzle_solver/puzzles/singles/singles.py,sha256=kwMENfqQ-OP3YIz5baY6LRcvYCsNfhImEXN00lwazKM,5658
|
26
27
|
puzzle_solver/puzzles/sudoku/sudoku.py,sha256=M_pry7XyKKzlfCF5rFi02lyOrj5GWZzXnDAxmD3NXvI,3588
|
27
28
|
puzzle_solver/puzzles/tents/tents.py,sha256=iyVK2WXfIT5j_9qqlQg0WmwvixwXlZSsHGK3XA-KpII,6283
|
29
|
+
puzzle_solver/puzzles/thermometers/thermometers.py,sha256=nsvJZkm7G8FALT27bpaB0lv5E_AWawqmvapQI8QcYXw,4015
|
28
30
|
puzzle_solver/puzzles/towers/towers.py,sha256=QvL0Pp-Z2ewCeq9ZkNrh8MShKOh-Y52sFBSudve68wk,6496
|
29
31
|
puzzle_solver/puzzles/tracks/tracks.py,sha256=VnAtxBkuUTHJYNXr1JGg0yYzJj3kRMBi8Nz7NHKS94A,9089
|
30
32
|
puzzle_solver/puzzles/undead/undead.py,sha256=ygNugW5SOlYLy6d740gZ2IW9UJQ3SAr9vuMm0ZFr2nY,6630
|
31
33
|
puzzle_solver/puzzles/unruly/unruly.py,sha256=sDF0oKT50G-NshyW2DYrvAgD9q9Ku9ANUyNhGSAu7cQ,3827
|
32
|
-
|
33
|
-
multi_puzzle_solver-0.9.
|
34
|
-
multi_puzzle_solver-0.9.
|
35
|
-
multi_puzzle_solver-0.9.
|
34
|
+
puzzle_solver/utils/visualizer.py,sha256=ImerGaFuWpuQqtb3infQ9__mFAHdf7ibEMYYxmIOLA0,4958
|
35
|
+
multi_puzzle_solver-0.9.3.dist-info/METADATA,sha256=KNUafonxjZw4KLdoe2kwtVn1asnbQmu1nS1JnX22zfM,113408
|
36
|
+
multi_puzzle_solver-0.9.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
37
|
+
multi_puzzle_solver-0.9.3.dist-info/top_level.txt,sha256=exwVUQa-anK9vYrpKzBPvH8bX43iElWI4VeNiAyBGJY,14
|
38
|
+
multi_puzzle_solver-0.9.3.dist-info/RECORD,,
|
puzzle_solver/__init__.py
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
from puzzle_solver.puzzles.aquarium import aquarium as aquarium_solver
|
1
2
|
from puzzle_solver.puzzles.bridges import bridges as bridges_solver
|
2
3
|
from puzzle_solver.puzzles.chess_range import chess_range as chess_range_solver
|
3
4
|
from puzzle_solver.puzzles.chess_range import chess_solo as chess_solo_solver
|
@@ -19,6 +20,7 @@ from puzzle_solver.puzzles.signpost import signpost as signpost_solver
|
|
19
20
|
from puzzle_solver.puzzles.singles import singles as singles_solver
|
20
21
|
from puzzle_solver.puzzles.sudoku import sudoku as sudoku_solver
|
21
22
|
from puzzle_solver.puzzles.tents import tents as tents_solver
|
23
|
+
from puzzle_solver.puzzles.thermometers import thermometers as thermometers_solver
|
22
24
|
from puzzle_solver.puzzles.towers import towers as towers_solver
|
23
25
|
from puzzle_solver.puzzles.tracks import tracks as tracks_solver
|
24
26
|
from puzzle_solver.puzzles.undead import undead as undead_solver
|
@@ -26,4 +28,4 @@ from puzzle_solver.puzzles.unruly import unruly as unruly_solver
|
|
26
28
|
|
27
29
|
from puzzle_solver.puzzles.inertia.parse_map.parse_map import main as inertia_image_parser
|
28
30
|
|
29
|
-
__version__ = '0.9.
|
31
|
+
__version__ = '0.9.3'
|
@@ -0,0 +1,112 @@
|
|
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_neighbors4, get_row_pos, get_col_pos
|
5
|
+
from puzzle_solver.core.utils_ortools import generic_solve_all, SingleSolution
|
6
|
+
|
7
|
+
|
8
|
+
def _sanity_check(board: np.array):
|
9
|
+
# percolation check
|
10
|
+
V = board.shape[0]
|
11
|
+
H = board.shape[1]
|
12
|
+
visited: set[Pos] = set()
|
13
|
+
finished_islands: set[int] = set()
|
14
|
+
def dfs(pos: Pos, target_i: int):
|
15
|
+
if pos in visited:
|
16
|
+
return
|
17
|
+
visited.add(pos)
|
18
|
+
for neighbor in get_neighbors4(pos, V, H):
|
19
|
+
if neighbor in visited:
|
20
|
+
continue
|
21
|
+
neighbor_i = int(get_char(board, neighbor))
|
22
|
+
if neighbor_i == target_i:
|
23
|
+
dfs(neighbor, target_i)
|
24
|
+
for pos in get_all_pos(V, H):
|
25
|
+
if pos in visited:
|
26
|
+
continue
|
27
|
+
current_i = int(get_char(board, pos))
|
28
|
+
assert current_i not in finished_islands, f'island {current_i} already finished'
|
29
|
+
dfs(pos, current_i)
|
30
|
+
finished_islands.add(current_i)
|
31
|
+
|
32
|
+
assert len(finished_islands) == len(set(board.flatten())), 'board is not connected'
|
33
|
+
|
34
|
+
class Board:
|
35
|
+
def __init__(self, board: np.array, top: np.array, side: np.array):
|
36
|
+
assert board.ndim == 2, f'board must be 2d, got {board.ndim}'
|
37
|
+
_sanity_check(board)
|
38
|
+
self.V = board.shape[0]
|
39
|
+
self.H = board.shape[1]
|
40
|
+
assert top.ndim == 1 and top.shape[0] == self.H, 'top must be a 1d array of length board width'
|
41
|
+
assert side.ndim == 1 and side.shape[0] == self.V, 'side must be a 1d array of length board height'
|
42
|
+
assert all((str(c.item()).isdecimal() for c in np.nditer(board))), 'board must contain only digits'
|
43
|
+
self.board = board
|
44
|
+
self.top = top
|
45
|
+
self.side = side
|
46
|
+
self.aquarium_numbers = set([int(c.item()) for c in np.nditer(board)])
|
47
|
+
self.aquariums = {i: [pos for pos in get_all_pos(self.V, self.H) if int(get_char(self.board, pos)) == i] for i in self.aquarium_numbers}
|
48
|
+
self.aquariums_exist_in_row: dict[int, set[int]] = {aq_i: set() for aq_i in self.aquarium_numbers}
|
49
|
+
for aq_i in self.aquarium_numbers:
|
50
|
+
for row in range(self.V):
|
51
|
+
if any(pos.y == row for pos in self.aquariums[aq_i]):
|
52
|
+
self.aquariums_exist_in_row[aq_i].add(row)
|
53
|
+
|
54
|
+
self.model = cp_model.CpModel()
|
55
|
+
self.model_vars: dict[Pos, cp_model.IntVar] = {}
|
56
|
+
self.is_aquarium_here: dict[tuple[int, int], cp_model.IntVar] = {} # is the aquarium here?
|
57
|
+
|
58
|
+
self.create_vars()
|
59
|
+
self.add_all_constraints()
|
60
|
+
|
61
|
+
def create_vars(self):
|
62
|
+
for pos in get_all_pos(self.V, self.H):
|
63
|
+
self.model_vars[pos] = self.model.NewBoolVar(f'{pos}')
|
64
|
+
for aq_i in self.aquarium_numbers:
|
65
|
+
for row in self.aquariums_exist_in_row[aq_i]:
|
66
|
+
self.is_aquarium_here[row, aq_i] = self.model.NewBoolVar(f'{row}:{aq_i}')
|
67
|
+
|
68
|
+
def add_all_constraints(self):
|
69
|
+
for aq_i in self.aquarium_numbers:
|
70
|
+
for pos in self.aquariums[aq_i]:
|
71
|
+
self.model.Add(self.is_aquarium_here[pos.y, aq_i] == 1).OnlyEnforceIf(self.model_vars[pos])
|
72
|
+
|
73
|
+
# aquarium always start from the bottom
|
74
|
+
for aq_i in self.aquarium_numbers:
|
75
|
+
for row in self.aquariums_exist_in_row[aq_i]:
|
76
|
+
# (row + 1) is below (row)
|
77
|
+
if row + 1 not in self.aquariums_exist_in_row[aq_i]:
|
78
|
+
continue
|
79
|
+
self.model.Add(self.is_aquarium_here[row + 1, aq_i] == 1).OnlyEnforceIf(self.is_aquarium_here[row, aq_i])
|
80
|
+
|
81
|
+
for row in range(self.V):
|
82
|
+
for aq_i in self.aquarium_numbers:
|
83
|
+
aq_i_row_pos = [pos for pos in self.aquariums[aq_i] if pos.y == row]
|
84
|
+
for pos in aq_i_row_pos:
|
85
|
+
# if the aquarium is here, all the squares in the row of this aquarium must be filled
|
86
|
+
self.model.Add(self.model_vars[pos] == 1).OnlyEnforceIf(self.is_aquarium_here[row, aq_i])
|
87
|
+
# if the aquarium is here, at least one square in the row of this aquarium must be filled
|
88
|
+
if len(aq_i_row_pos) > 0:
|
89
|
+
self.model.Add(sum([self.model_vars[pos] for pos in aq_i_row_pos]) == len(aq_i_row_pos)).OnlyEnforceIf(self.is_aquarium_here[row, aq_i])
|
90
|
+
self.model.Add(sum([self.model_vars[pos] for pos in aq_i_row_pos]) == 0).OnlyEnforceIf(self.is_aquarium_here[row, aq_i].Not())
|
91
|
+
|
92
|
+
# force the top and side constraints
|
93
|
+
for col in range(self.H):
|
94
|
+
self.model.Add(sum([self.model_vars[pos] for pos in get_col_pos(col, self.V)]) == self.top[col])
|
95
|
+
for row in range(self.V):
|
96
|
+
self.model.Add(sum([self.model_vars[pos] for pos in get_row_pos(row, self.H)]) == self.side[row])
|
97
|
+
|
98
|
+
def solve_and_print(self, verbose: bool = True):
|
99
|
+
def board_to_solution(board: Board, solver: cp_model.CpSolverSolutionCallback) -> SingleSolution:
|
100
|
+
assignment: dict[Pos, int] = {}
|
101
|
+
for pos, var in board.model_vars.items():
|
102
|
+
assignment[pos] = solver.value(var)
|
103
|
+
return SingleSolution(assignment=assignment)
|
104
|
+
def callback(single_res: SingleSolution):
|
105
|
+
print("Solution found")
|
106
|
+
res = np.full((self.V, self.H), ' ', dtype=str)
|
107
|
+
for pos, val in single_res.assignment.items():
|
108
|
+
c = str(val)
|
109
|
+
set_char(res, pos, c)
|
110
|
+
print(res)
|
111
|
+
|
112
|
+
return generic_solve_all(self, board_to_solution, callback=callback if verbose else None, verbose=verbose, max_solutions=99)
|
@@ -0,0 +1,82 @@
|
|
1
|
+
from typing import Optional
|
2
|
+
|
3
|
+
import numpy as np
|
4
|
+
from ortools.sat.python import cp_model
|
5
|
+
|
6
|
+
from puzzle_solver.core.utils import Pos, get_all_pos, get_char, set_char, Direction, get_opposite_direction, get_next_pos, in_bounds, get_row_pos, get_col_pos
|
7
|
+
from puzzle_solver.core.utils_ortools import generic_solve_all, SingleSolution
|
8
|
+
|
9
|
+
def get_direction(pos: Pos, board: np.array) -> Optional[Direction]:
|
10
|
+
if get_char(board, pos) == 'R':
|
11
|
+
return Direction.RIGHT
|
12
|
+
elif get_char(board, pos) == 'D':
|
13
|
+
return Direction.DOWN
|
14
|
+
elif get_char(board, pos) == 'U':
|
15
|
+
return Direction.UP
|
16
|
+
elif get_char(board, pos) == 'L':
|
17
|
+
return Direction.LEFT
|
18
|
+
return None
|
19
|
+
|
20
|
+
def move_backward(pos: Pos, board: np.array) -> Pos:
|
21
|
+
for direction in Direction:
|
22
|
+
opposite_direction = get_opposite_direction(direction)
|
23
|
+
neighbor = get_next_pos(pos, direction)
|
24
|
+
if in_bounds(neighbor, board.shape[0], board.shape[1]) and get_direction(neighbor, board) == opposite_direction: # the neighbor is pointing to me
|
25
|
+
return neighbor
|
26
|
+
return None
|
27
|
+
|
28
|
+
class Board:
|
29
|
+
def __init__(self, board: np.array, top: np.array, side: np.array):
|
30
|
+
assert board.ndim == 2, f'board must be 2d, got {board.ndim}'
|
31
|
+
self.V = board.shape[0]
|
32
|
+
self.H = board.shape[1]
|
33
|
+
assert top.ndim == 1 and top.shape[0] == self.H, 'top must be a 1d array of length board width'
|
34
|
+
assert side.ndim == 1 and side.shape[0] == self.V, 'side must be a 1d array of length board height'
|
35
|
+
assert all((c in ['R', 'D', 'U', 'X', 'L']) for c in np.nditer(board)), 'board must contain only valid characters: R, D, U, X, L'
|
36
|
+
self.board = board
|
37
|
+
self.top = top
|
38
|
+
self.side = side
|
39
|
+
self.tip: set[Pos] = {pos for pos in get_all_pos(self.V, self.H) if get_char(self.board, pos) == 'X'}
|
40
|
+
|
41
|
+
self.model = cp_model.CpModel()
|
42
|
+
self.model_vars: dict[Pos, cp_model.IntVar] = {}
|
43
|
+
|
44
|
+
self.create_vars()
|
45
|
+
self.add_all_constraints()
|
46
|
+
|
47
|
+
def create_vars(self):
|
48
|
+
for pos in get_all_pos(self.V, self.H):
|
49
|
+
self.model_vars[pos] = self.model.NewBoolVar(f'{pos}')
|
50
|
+
|
51
|
+
def add_all_constraints(self):
|
52
|
+
visited: set[Pos] = set()
|
53
|
+
for cur_pos in self.tip:
|
54
|
+
visited.add(cur_pos)
|
55
|
+
while cur_pos is not None:
|
56
|
+
backward_pos = move_backward(cur_pos, self.board)
|
57
|
+
if backward_pos is None:
|
58
|
+
break
|
59
|
+
self.model.Add(self.model_vars[backward_pos] == 1).OnlyEnforceIf(self.model_vars[cur_pos])
|
60
|
+
cur_pos = backward_pos
|
61
|
+
visited.add(cur_pos)
|
62
|
+
assert len(visited) == self.V * self.H, f'all positions must be visited, got {len(visited)}. missing {set(get_all_pos(self.V, self.H)) - visited}'
|
63
|
+
for row in range(self.V):
|
64
|
+
self.model.Add(sum([self.model_vars[pos] for pos in get_row_pos(row, self.H)]) == self.side[row])
|
65
|
+
for col in range(self.H):
|
66
|
+
self.model.Add(sum([self.model_vars[pos] for pos in get_col_pos(col, self.V)]) == self.top[col])
|
67
|
+
|
68
|
+
def solve_and_print(self, verbose: bool = True):
|
69
|
+
def board_to_solution(board: Board, solver: cp_model.CpSolverSolutionCallback) -> SingleSolution:
|
70
|
+
assignment: dict[Pos, int] = {}
|
71
|
+
for pos, var in board.model_vars.items():
|
72
|
+
assignment[pos] = solver.value(var)
|
73
|
+
return SingleSolution(assignment=assignment)
|
74
|
+
def callback(single_res: SingleSolution):
|
75
|
+
print("Solution found")
|
76
|
+
res = np.full((self.V, self.H), ' ', dtype=object)
|
77
|
+
for pos in get_all_pos(self.V, self.H):
|
78
|
+
c = get_char(self.board, pos)
|
79
|
+
c = 'X' if single_res.assignment[pos] == 1 else ' '
|
80
|
+
set_char(res, pos, c)
|
81
|
+
print(res)
|
82
|
+
return generic_solve_all(self, board_to_solution, callback=callback if verbose else None, verbose=verbose)
|
@@ -0,0 +1,108 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
from typing import Any, Mapping, Tuple
|
3
|
+
|
4
|
+
import numpy as np
|
5
|
+
from PIL import Image, ImageDraw, ImageFont
|
6
|
+
|
7
|
+
RGB = Tuple[int, int, int]
|
8
|
+
|
9
|
+
def render_board_image(
|
10
|
+
board: np.ndarray,
|
11
|
+
colors: Mapping[Any, RGB],
|
12
|
+
output_filename: str,
|
13
|
+
cell_size: int = 64,
|
14
|
+
grid: bool = True,
|
15
|
+
bg_default: RGB = (240, 240, 240),
|
16
|
+
text_color: RGB = (0, 0, 0),
|
17
|
+
padding: int = 20,
|
18
|
+
) -> None:
|
19
|
+
"""
|
20
|
+
Render a 2D numpy array as a colored grid image with centered text labels.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
board: 2D numpy array (dtype can be object/str/int/etc.). Each cell's value
|
24
|
+
is looked up in `colors` for its fill color.
|
25
|
+
colors: Dict-like mapping from cell values to RGB tuples (0-255).
|
26
|
+
output_filename: Where to save the image (e.g., 'board.png').
|
27
|
+
cell_size: Square side length (pixels) for each cell.
|
28
|
+
grid: Whether to draw grid lines around cells.
|
29
|
+
bg_default: Fill color when a cell's value is not in `colors`.
|
30
|
+
text_color: Color for text labels.
|
31
|
+
padding: Extra pixels around the entire grid on each side.
|
32
|
+
"""
|
33
|
+
if board.ndim != 2:
|
34
|
+
raise ValueError("`board` must be a 2D numpy array.")
|
35
|
+
|
36
|
+
rows, cols = board.shape
|
37
|
+
width = cols * cell_size + padding * 2
|
38
|
+
height = rows * cell_size + padding * 2
|
39
|
+
|
40
|
+
img = Image.new("RGB", (width, height), color=bg_default)
|
41
|
+
draw = ImageDraw.Draw(img)
|
42
|
+
|
43
|
+
# font size
|
44
|
+
desired_pt = max(10, int(cell_size * 0.4))
|
45
|
+
font = ImageFont.truetype("DejaVuSans.ttf", desired_pt)
|
46
|
+
|
47
|
+
missing_values = set()
|
48
|
+
for r in range(rows):
|
49
|
+
for c in range(cols):
|
50
|
+
val = board[r, c]
|
51
|
+
if isinstance(val, np.generic):
|
52
|
+
val = val.item()
|
53
|
+
if val not in colors:
|
54
|
+
print(f'missing value: {val} of type {type(val)} at position {r}, {c}')
|
55
|
+
missing_values.add(val)
|
56
|
+
fill = colors.get(val, bg_default)
|
57
|
+
|
58
|
+
x0 = padding + c * cell_size
|
59
|
+
y0 = padding + r * cell_size
|
60
|
+
x1 = x0 + cell_size
|
61
|
+
y1 = y0 + cell_size
|
62
|
+
|
63
|
+
draw.rectangle([x0, y0, x1, y1], fill=fill)
|
64
|
+
|
65
|
+
if grid:
|
66
|
+
draw.rectangle([x0, y0, x1, y1], outline=(0, 0, 0), width=1)
|
67
|
+
|
68
|
+
# Center the text
|
69
|
+
text = "" if val is None else str(val)
|
70
|
+
if text:
|
71
|
+
# textbbox gives more accurate sizing than textsize
|
72
|
+
bbox = draw.textbbox((0, 0), text, font=font)
|
73
|
+
tw = bbox[2] - bbox[0]
|
74
|
+
th = bbox[3] - bbox[1]
|
75
|
+
tx = x0 + (cell_size - tw) / 2
|
76
|
+
ty = y0 + (cell_size - th) / 2
|
77
|
+
draw.text((tx, ty), text, fill=text_color, font=font)
|
78
|
+
|
79
|
+
img.save(output_filename)
|
80
|
+
|
81
|
+
|
82
|
+
if __name__ == '__main__':
|
83
|
+
board = np.array([
|
84
|
+
['01', '01', '01', '01', '02', '02', '02', '03', '03', '03', '03', '04', '05', '05', '05'],
|
85
|
+
['01', '02', '02', '02', '02', '06', '07', '07', '03', '08', '03', '04', '04', '05', '09'],
|
86
|
+
['01', '01', '02', '11', '06', '06', '06', '12', '12', '08', '13', '13', '13', '09', '09'],
|
87
|
+
['01', '11', '11', '11', '14', '06', '06', '12', '12', '15', '15', '13', '09', '09', '09'],
|
88
|
+
['01', '01', '11', '11', '14', '12', '12', '12', '16', '16', '15', '13', '13', '17', '09'],
|
89
|
+
['01', '11', '11', '14', '14', '12', '42', '42', '42', '15', '15', '13', '13', '17', '18'],
|
90
|
+
['01', '11', '11', '14', '14', '12', '12', '43', '15', '15', '20', '13', '13', '17', '18'],
|
91
|
+
['01', '01', '11', '19', '19', '19', '43', '43', '44', '20', '20', '20', '13', '17', '18'],
|
92
|
+
['01', '22', '23', '23', '23', '19', '43', '21', '21', '24', '24', '24', '25', '17', '17'],
|
93
|
+
['22', '22', '22', '23', '19', '19', '26', '24', '24', '24', '28', '28', '25', '17', '33'],
|
94
|
+
['22', '22', '23', '23', '27', '27', '26', '26', '24', '24', '29', '29', '25', '25', '33'],
|
95
|
+
['22', '22', '35', '27', '27', '26', '26', '26', '26', '30', '30', '30', '25', '34', '34'],
|
96
|
+
['37', '22', '35', '35', '35', '35', '35', '26', '26', '30', '31', '31', '32', '32', '40'],
|
97
|
+
['37', '37', '37', '36', '36', '35', '26', '26', '26', '40', '40', '40', '40', '40', '40'],
|
98
|
+
['37', '37', '37', '37', '35', '35', '38', '38', '39', '39', '40', '40', '40', '41', '41'],
|
99
|
+
])
|
100
|
+
# rcolors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255), (255, 255, 255), (128, 128, 128)]
|
101
|
+
vs =[0, 255]
|
102
|
+
rcolors = [(v1, v2, v3) for v1 in vs for v2 in vs for v3 in vs if (v1, v2, v3) != (0, 0, 0)]
|
103
|
+
nums = set([c.item() for c in np.nditer(board)])
|
104
|
+
colors = {i: rcolors[int(i) % len(rcolors)] for i in nums}
|
105
|
+
print(nums)
|
106
|
+
print('max i:', max(nums))
|
107
|
+
print('skipped:', set(range(int(max(nums)) + 1)) - set(int(i) for i in nums))
|
108
|
+
render_board_image(board, colors, 'board.png')
|
File without changes
|
File without changes
|