multi-puzzle-solver 1.0.6__py3-none-any.whl → 1.0.8__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.
Potentially problematic release.
This version of multi-puzzle-solver might be problematic. Click here for more details.
- {multi_puzzle_solver-1.0.6.dist-info → multi_puzzle_solver-1.0.8.dist-info}/METADATA +421 -265
- {multi_puzzle_solver-1.0.6.dist-info → multi_puzzle_solver-1.0.8.dist-info}/RECORD +13 -11
- puzzle_solver/__init__.py +5 -1
- puzzle_solver/core/utils_visualizer.py +565 -561
- puzzle_solver/puzzles/abc_view/abc_view.py +75 -0
- puzzle_solver/puzzles/heyawake/heyawake.py +67 -13
- puzzle_solver/puzzles/mathema_grids/mathema_grids.py +119 -0
- puzzle_solver/puzzles/nonograms/nonograms_colored.py +220 -221
- puzzle_solver/puzzles/palisade/palisade.py +91 -106
- puzzle_solver/puzzles/shingoki/shingoki.py +61 -104
- puzzle_solver/puzzles/tracks/tracks.py +1 -1
- {multi_puzzle_solver-1.0.6.dist-info → multi_puzzle_solver-1.0.8.dist-info}/WHEEL +0 -0
- {multi_puzzle_solver-1.0.6.dist-info → multi_puzzle_solver-1.0.8.dist-info}/top_level.txt +0 -0
|
@@ -1,106 +1,91 @@
|
|
|
1
|
-
from dataclasses import dataclass
|
|
2
|
-
from collections import defaultdict
|
|
3
|
-
|
|
4
|
-
import numpy as np
|
|
5
|
-
from ortools.sat.python import cp_model
|
|
6
|
-
|
|
7
|
-
from puzzle_solver.core.utils import Pos, get_all_pos, get_pos,
|
|
8
|
-
from puzzle_solver.core.utils_ortools import generic_solve_all, SingleSolution
|
|
9
|
-
from puzzle_solver.core.utils_visualizer import combined_function, id_board_to_wall_fn
|
|
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
|
-
self.board
|
|
58
|
-
self.
|
|
59
|
-
|
|
60
|
-
self.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
self.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
self.
|
|
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
|
-
for shape in board.shapes_on_board:
|
|
93
|
-
if solver.Value(shape.is_active) == 1:
|
|
94
|
-
for pos in shape.body:
|
|
95
|
-
assignment[pos] = shape.shape_id
|
|
96
|
-
return SingleSolution(assignment=assignment)
|
|
97
|
-
def callback(single_res: SingleSolution):
|
|
98
|
-
print("Solution found")
|
|
99
|
-
id_board = np.full((self.V, self.H), ' ', dtype=object)
|
|
100
|
-
for pos in get_all_pos(self.V, self.H):
|
|
101
|
-
region_idx = single_res.assignment[pos]
|
|
102
|
-
set_char(id_board, pos, region_idx)
|
|
103
|
-
print(combined_function(self.V, self.H,
|
|
104
|
-
cell_flags=id_board_to_wall_fn(id_board),
|
|
105
|
-
center_char=lambda r, c: self.board[r, c] if self.board[r, c] != ' ' else '·'))
|
|
106
|
-
return generic_solve_all(self, board_to_solution, callback=callback if verbose else None, verbose=verbose)
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from collections import defaultdict
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from ortools.sat.python import cp_model
|
|
6
|
+
|
|
7
|
+
from puzzle_solver.core.utils import Pos, get_all_pos, get_pos, in_bounds, get_next_pos, Direction, polyominoes
|
|
8
|
+
from puzzle_solver.core.utils_ortools import generic_solve_all, SingleSolution
|
|
9
|
+
from puzzle_solver.core.utils_visualizer import combined_function, id_board_to_wall_fn
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Shape = frozenset[Pos] # a shape on the 2d board is just a set of positions
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class ShapeOnBoard:
|
|
16
|
+
is_active: cp_model.IntVar
|
|
17
|
+
shape: Shape
|
|
18
|
+
shape_id: int
|
|
19
|
+
body: set[Pos]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_valid_translations(shape: Shape, board: np.array) -> set[Pos]:
|
|
23
|
+
# give a shape and a board, return all valid translations of the shape that are fully contained in the board AND consistent with the clues on the board
|
|
24
|
+
shape_list = list(shape)
|
|
25
|
+
shape_borders = [] # will contain the number of borders for each pos in the shape; this has to be consistent with the clues on the board
|
|
26
|
+
for pos in shape_list:
|
|
27
|
+
v = 0
|
|
28
|
+
for direction in Direction:
|
|
29
|
+
next_pos = get_next_pos(pos, direction)
|
|
30
|
+
if not in_bounds(next_pos, board.shape[0], board.shape[1]) or next_pos not in shape:
|
|
31
|
+
v += 1
|
|
32
|
+
shape_borders.append(v)
|
|
33
|
+
shape_list = [(p.x, p.y) for p in shape_list]
|
|
34
|
+
# min x/y is always 0
|
|
35
|
+
max_x = max(p[0] for p in shape_list)
|
|
36
|
+
max_y = max(p[1] for p in shape_list)
|
|
37
|
+
for dy in range(0, board.shape[0] - max_y):
|
|
38
|
+
for dx in range(0, board.shape[1] - max_x):
|
|
39
|
+
body = tuple((p[0] + dx, p[1] + dy) for p in shape_list)
|
|
40
|
+
for i, p in enumerate(body):
|
|
41
|
+
c = board[p[1], p[0]]
|
|
42
|
+
if c != ' ' and c != str(shape_borders[i]): # there is a clue and it doesn't match my translated shape, skip
|
|
43
|
+
break
|
|
44
|
+
else:
|
|
45
|
+
yield frozenset(get_pos(x=p[0], y=p[1]) for p in body)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Board:
|
|
49
|
+
def __init__(self, board: np.array, region_size: int):
|
|
50
|
+
assert region_size >= 1 and isinstance(region_size, int), 'region_size must be an integer greater than or equal to 1'
|
|
51
|
+
assert board.ndim == 2, f'board must be 2d, got {board.ndim}'
|
|
52
|
+
assert all((c.item() == ' ') or str(c.item()).isdecimal() for c in np.nditer(board)), 'board must contain only space or digits'
|
|
53
|
+
self.board = board
|
|
54
|
+
self.V, self.H = board.shape
|
|
55
|
+
self.region_size = region_size
|
|
56
|
+
self.region_count = (self.V * self.H) // self.region_size
|
|
57
|
+
assert self.region_count * self.region_size == self.V * self.H, f'region_size must be a factor of the board size, got {self.region_size} and {self.region_count}'
|
|
58
|
+
self.polyominoes = polyominoes(self.region_size)
|
|
59
|
+
|
|
60
|
+
self.model = cp_model.CpModel()
|
|
61
|
+
self.shapes_on_board: list[ShapeOnBoard] = [] # will contain every possible shape on the board based on polyomino degrees
|
|
62
|
+
self.pos_to_shapes: dict[Pos, set[ShapeOnBoard]] = defaultdict(set)
|
|
63
|
+
self.create_vars()
|
|
64
|
+
self.add_all_constraints()
|
|
65
|
+
|
|
66
|
+
def create_vars(self):
|
|
67
|
+
for shape in self.polyominoes:
|
|
68
|
+
for body in get_valid_translations(shape, self.board):
|
|
69
|
+
uid = len(self.shapes_on_board)
|
|
70
|
+
shape_on_board = ShapeOnBoard(
|
|
71
|
+
is_active=self.model.NewBoolVar(f'{uid}:is_active'),
|
|
72
|
+
shape=shape, shape_id=uid, body=body
|
|
73
|
+
)
|
|
74
|
+
self.shapes_on_board.append(shape_on_board)
|
|
75
|
+
for pos in body:
|
|
76
|
+
self.pos_to_shapes[pos].add(shape_on_board)
|
|
77
|
+
|
|
78
|
+
def add_all_constraints(self):
|
|
79
|
+
for pos in get_all_pos(self.V, self.H): # each position has exactly one shape active
|
|
80
|
+
self.model.AddExactlyOne(shape.is_active for shape in self.pos_to_shapes[pos])
|
|
81
|
+
|
|
82
|
+
def solve_and_print(self, verbose: bool = True):
|
|
83
|
+
def board_to_solution(board: Board, solver: cp_model.CpSolverSolutionCallback) -> SingleSolution:
|
|
84
|
+
active_shapes = [shape for shape in board.shapes_on_board if solver.Value(shape.is_active) == 1]
|
|
85
|
+
return SingleSolution(assignment={pos: shape.shape_id for shape in active_shapes for pos in shape.body})
|
|
86
|
+
def callback(single_res: SingleSolution):
|
|
87
|
+
print("Solution found")
|
|
88
|
+
print(combined_function(self.V, self.H,
|
|
89
|
+
cell_flags=id_board_to_wall_fn(np.array([[single_res.assignment[get_pos(x=c, y=r)] for c in range(self.H)] for r in range(self.V)])),
|
|
90
|
+
center_char=lambda r, c: self.board[r, c] if self.board[r, c] != ' ' else '·'))
|
|
91
|
+
return generic_solve_all(self, board_to_solution, callback=callback if verbose else None, verbose=verbose)
|
|
@@ -1,35 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
from dataclasses import dataclass
|
|
3
|
-
import time
|
|
1
|
+
from collections import defaultdict
|
|
4
2
|
|
|
5
3
|
import numpy as np
|
|
6
4
|
from ortools.sat.python import cp_model
|
|
7
5
|
|
|
8
|
-
from puzzle_solver.core.utils import Direction, Pos, get_all_pos,
|
|
9
|
-
from puzzle_solver.core.utils_ortools import generic_solve_all, force_connected_component, and_constraint
|
|
6
|
+
from puzzle_solver.core.utils import Direction, Pos, get_all_pos, get_next_pos, get_char, in_bounds, set_char, get_pos, get_opposite_direction
|
|
7
|
+
from puzzle_solver.core.utils_ortools import generic_solve_all, force_connected_component, and_constraint, SingleSolution
|
|
10
8
|
from puzzle_solver.core.utils_visualizer import combined_function
|
|
11
9
|
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
class SingleSolution:
|
|
15
|
-
assignment: dict[tuple[Pos, Pos], int]
|
|
16
|
-
|
|
17
|
-
def get_hashable_solution(self) -> str:
|
|
18
|
-
result = []
|
|
19
|
-
for (pos, neighbor), v in self.assignment.items():
|
|
20
|
-
result.append((pos.x, pos.y, neighbor.x, neighbor.y, v))
|
|
21
|
-
return json.dumps(result, sort_keys=True)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def get_ray(pos: Pos, V: int, H: int, direction: Direction) -> list[tuple[Pos, Pos]]:
|
|
11
|
+
def get_ray(pos: Pos, V: int, H: int, direction: Direction) -> list[Pos]:
|
|
25
12
|
out = []
|
|
26
|
-
prev_pos = pos
|
|
27
13
|
while True:
|
|
14
|
+
out.append(pos)
|
|
28
15
|
pos = get_next_pos(pos, direction)
|
|
29
16
|
if not in_bounds(pos, V, H):
|
|
30
17
|
break
|
|
31
|
-
out.append((prev_pos, pos))
|
|
32
|
-
prev_pos = pos
|
|
33
18
|
return out
|
|
34
19
|
|
|
35
20
|
|
|
@@ -37,8 +22,8 @@ class Board:
|
|
|
37
22
|
def __init__(self, board: np.array):
|
|
38
23
|
assert board.ndim == 2, f'board must be 2d, got {board.ndim}'
|
|
39
24
|
assert all((c.item().strip() == '') or (str(c.item())[:-1].isdecimal() and c.item()[-1].upper() in ['B', 'W']) for c in np.nditer(board)), 'board must contain only space or digits and B/W'
|
|
40
|
-
|
|
41
25
|
self.V, self.H = board.shape
|
|
26
|
+
self.board = board
|
|
42
27
|
self.board_numbers: dict[Pos, int] = {}
|
|
43
28
|
self.board_colors: dict[Pos, str] = {}
|
|
44
29
|
for pos in get_all_pos(self.V, self.H):
|
|
@@ -47,112 +32,84 @@ class Board:
|
|
|
47
32
|
continue
|
|
48
33
|
self.board_numbers[pos] = int(c[:-1])
|
|
49
34
|
self.board_colors[pos] = c[-1].upper()
|
|
35
|
+
|
|
50
36
|
self.model = cp_model.CpModel()
|
|
51
|
-
self.
|
|
37
|
+
self.cell_active: dict[Pos, cp_model.IntVar] = {}
|
|
38
|
+
self.cell_direction: dict[tuple[Pos, Direction], cp_model.IntVar] = {}
|
|
52
39
|
|
|
53
40
|
self.create_vars()
|
|
54
41
|
self.add_all_constraints()
|
|
55
42
|
|
|
56
43
|
def create_vars(self):
|
|
57
44
|
for pos in get_all_pos(self.V, self.H):
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
45
|
+
self.cell_active[pos] = self.model.NewBoolVar(f'{pos}')
|
|
46
|
+
for direction in Direction:
|
|
47
|
+
neighbor = get_next_pos(pos, direction)
|
|
48
|
+
opposite_direction = get_opposite_direction(direction)
|
|
49
|
+
if not in_bounds(neighbor, self.V, self.H):
|
|
50
|
+
self.cell_direction[(pos, direction)] = self.model.NewConstant(0)
|
|
51
|
+
continue
|
|
52
|
+
if (neighbor, opposite_direction) in self.cell_direction:
|
|
53
|
+
self.cell_direction[(pos, direction)] = self.cell_direction[(neighbor, opposite_direction)]
|
|
54
|
+
else:
|
|
55
|
+
self.cell_direction[(pos, direction)] = self.model.NewBoolVar(f'{pos}-{neighbor}')
|
|
63
56
|
|
|
64
57
|
def add_all_constraints(self):
|
|
65
|
-
# each corners must have either 0 or 2 neighbors
|
|
66
|
-
for pos in get_all_pos(self.V, self.H):
|
|
67
|
-
corner_connections = [self.edge_vars[(pos, n)] for n in get_neighbors4(pos, self.V, self.H)]
|
|
68
|
-
if pos not in self.board_numbers: # no color, either 0 or 2 edges
|
|
69
|
-
self.model.AddLinearExpressionInDomain(sum(corner_connections), cp_model.Domain.FromValues([0, 2]))
|
|
70
|
-
else: # color, must have exactly 2 edges
|
|
71
|
-
self.model.Add(sum(corner_connections) == 2)
|
|
72
|
-
|
|
73
|
-
# enforce colors
|
|
74
58
|
for pos in get_all_pos(self.V, self.H):
|
|
59
|
+
s = sum([self.cell_direction[(pos, direction)] for direction in Direction])
|
|
60
|
+
self.model.Add(s == 2).OnlyEnforceIf(self.cell_active[pos])
|
|
61
|
+
self.model.Add(s == 0).OnlyEnforceIf(self.cell_active[pos].Not())
|
|
75
62
|
if pos not in self.board_numbers:
|
|
76
63
|
continue
|
|
77
|
-
self.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
# enforce single connected component
|
|
81
|
-
def is_neighbor(edge1: tuple[Pos, Pos], edge2: tuple[Pos, Pos]) -> bool:
|
|
82
|
-
return any(c1 == c2 for c1 in edge1 for c2 in edge2)
|
|
83
|
-
force_connected_component(self.model, self.edge_vars, is_neighbor=is_neighbor)
|
|
64
|
+
self.enforce_corner_color_and_number(pos, self.board_colors[pos], self.board_numbers[pos]) # enforce colors and number
|
|
65
|
+
self.force_connected_component() # enforce single connected component
|
|
84
66
|
|
|
85
|
-
def
|
|
86
|
-
assert pos_color in ['W', 'B'], f'Invalid color: {pos_color}'
|
|
87
|
-
|
|
88
|
-
var_r = self.edge_vars[(pos, pos_r)] if (pos, pos_r) in self.edge_vars else False
|
|
89
|
-
pos_d = get_next_pos(pos, Direction.DOWN)
|
|
90
|
-
var_d = self.edge_vars[(pos, pos_d)] if (pos, pos_d) in self.edge_vars else False
|
|
91
|
-
pos_l = get_next_pos(pos, Direction.LEFT)
|
|
92
|
-
var_l = self.edge_vars[(pos, pos_l)] if (pos, pos_l) in self.edge_vars else False
|
|
93
|
-
pos_u = get_next_pos(pos, Direction.UP)
|
|
94
|
-
var_u = self.edge_vars[(pos, pos_u)] if (pos, pos_u) in self.edge_vars else False
|
|
67
|
+
def enforce_corner_color_and_number(self, pos: Pos, pos_color: str, pos_number: int):
|
|
68
|
+
assert pos_color in ['W', 'B'] and pos_number > 0, f'Invalid color or number: {pos_color}, {pos_number}'
|
|
69
|
+
self.model.Add(self.cell_active[pos] == 1)
|
|
95
70
|
if pos_color == 'W': # White circles must be passed through in a straight line
|
|
96
|
-
self.model.Add(
|
|
97
|
-
self.model.Add(
|
|
71
|
+
self.model.Add(self.cell_direction[(pos, Direction.RIGHT)] == self.cell_direction[(pos, Direction.LEFT)])
|
|
72
|
+
self.model.Add(self.cell_direction[(pos, Direction.DOWN)] == self.cell_direction[(pos, Direction.UP)])
|
|
98
73
|
elif pos_color == 'B': # Black circles must be turned upon
|
|
99
|
-
self.model.Add(
|
|
100
|
-
self.model.Add(
|
|
101
|
-
self.model.Add(
|
|
102
|
-
self.model.Add(
|
|
74
|
+
self.model.Add(self.cell_direction[(pos, Direction.RIGHT)] == 0).OnlyEnforceIf([self.cell_direction[(pos, Direction.LEFT)]])
|
|
75
|
+
self.model.Add(self.cell_direction[(pos, Direction.LEFT)] == 0).OnlyEnforceIf([self.cell_direction[(pos, Direction.RIGHT)]])
|
|
76
|
+
self.model.Add(self.cell_direction[(pos, Direction.DOWN)] == 0).OnlyEnforceIf([self.cell_direction[(pos, Direction.UP)]])
|
|
77
|
+
self.model.Add(self.cell_direction[(pos, Direction.UP)] == 0).OnlyEnforceIf([self.cell_direction[(pos, Direction.DOWN)]])
|
|
103
78
|
else:
|
|
104
79
|
raise ValueError(f'Invalid color: {pos_color}')
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
rays = get_ray(pos, self.V, self.H, direction) # cells outward
|
|
112
|
-
if not rays:
|
|
113
|
-
continue
|
|
114
|
-
# Chain: v0 = w[ray[0]]; vt = w[ray[t]] & vt-1
|
|
115
|
-
prev = None
|
|
116
|
-
for idx, (pos1, pos2) in enumerate(rays):
|
|
117
|
-
v = self.model.NewBoolVar(f"vis[{pos1}-{pos2}]->({direction.name})[{idx}]")
|
|
80
|
+
vis_vars: list[cp_model.IntVar] = [] # The numbers in the circles show the sum of the lengths of the 2 straight lines going out of that circle.
|
|
81
|
+
for direction in Direction: # Build visibility chains in four direction
|
|
82
|
+
ray = get_ray(pos, self.V, self.H, direction) # cells outward
|
|
83
|
+
for idx in range(len(ray)):
|
|
84
|
+
v = self.model.NewBoolVar(f"vis[{pos}]->({direction.name})[{idx}]")
|
|
85
|
+
and_constraint(self.model, target=v, cs=[self.cell_direction[(p, direction)] for p in ray[:idx+1]])
|
|
118
86
|
vis_vars.append(v)
|
|
119
|
-
if idx == 0:
|
|
120
|
-
# v0 == w[cell]
|
|
121
|
-
self.model.Add(v == self.edge_vars[(pos1, pos2)])
|
|
122
|
-
else:
|
|
123
|
-
and_constraint(self.model, target=v, cs=[self.edge_vars[(pos1, pos2)], prev])
|
|
124
|
-
prev = v
|
|
125
87
|
self.model.Add(sum(vis_vars) == pos_number)
|
|
126
88
|
|
|
89
|
+
def force_connected_component(self):
|
|
90
|
+
def is_neighbor(pd1: tuple[Pos, Direction], pd2: tuple[Pos, Direction]) -> bool:
|
|
91
|
+
p1, d1 = pd1
|
|
92
|
+
p2, d2 = pd2
|
|
93
|
+
if p1 == p2 and d1 != d2: # same position, different direction, is neighbor
|
|
94
|
+
return True
|
|
95
|
+
if get_next_pos(p1, d1) == p2 and d2 == get_opposite_direction(d1):
|
|
96
|
+
return True
|
|
97
|
+
return False
|
|
98
|
+
force_connected_component(self.model, self.cell_direction, is_neighbor=is_neighbor)
|
|
127
99
|
|
|
128
100
|
def solve_and_print(self, verbose: bool = True):
|
|
129
|
-
tic = time.time()
|
|
130
101
|
def board_to_solution(board: Board, solver: cp_model.CpSolverSolutionCallback) -> SingleSolution:
|
|
131
|
-
assignment: dict[
|
|
132
|
-
for (pos,
|
|
133
|
-
assignment[
|
|
102
|
+
assignment: dict[Pos, str] = defaultdict(str)
|
|
103
|
+
for (pos, direction), var in board.cell_direction.items():
|
|
104
|
+
assignment[pos] += direction.name[0] if solver.BooleanValue(var) else ''
|
|
134
105
|
return SingleSolution(assignment=assignment)
|
|
135
106
|
def callback(single_res: SingleSolution):
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
min_y = min(pos.y, neighbor.y)
|
|
145
|
-
dx = abs(pos.x - neighbor.x)
|
|
146
|
-
dy = abs(pos.y - neighbor.y)
|
|
147
|
-
if min_x == self.H - 1: # only way to get right
|
|
148
|
-
res[min_y][min_x - 1] += 'R'
|
|
149
|
-
elif min_y == self.V - 1: # only way to get down
|
|
150
|
-
res[min_y - 1][min_x] += 'D'
|
|
151
|
-
elif dx == 1:
|
|
152
|
-
res[min_y][min_x] += 'U'
|
|
153
|
-
elif dy == 1:
|
|
154
|
-
res[min_y][min_x] += 'L'
|
|
155
|
-
else:
|
|
156
|
-
raise ValueError(f'Invalid position: {pos} and {neighbor}')
|
|
157
|
-
print(combined_function(self.V - 1, self.H - 1, cell_flags=lambda r, c: res[r, c], center_char=lambda r, c: '.'))
|
|
107
|
+
print("Solution found")
|
|
108
|
+
output_board = np.full((self.V, self.H), '', dtype=object)
|
|
109
|
+
for pos in get_all_pos(self.V, self.H):
|
|
110
|
+
if get_char(self.board, pos)[-1] in ['B', 'W']: # if the main board has a white or black pearl, put it in the output
|
|
111
|
+
set_char(output_board, pos, get_char(self.board, pos))
|
|
112
|
+
if not single_res.assignment[pos].strip(): # if the cell does not the line through it, put a dot
|
|
113
|
+
set_char(output_board, pos, '.')
|
|
114
|
+
print(combined_function(self.V, self.H, show_grid=False, special_content=lambda r, c: single_res.assignment[get_pos(x=c, y=r)], center_char=lambda r, c: output_board[r, c]))
|
|
158
115
|
return generic_solve_all(self, board_to_solution, callback=callback if verbose else None, verbose=verbose)
|
|
@@ -2,7 +2,7 @@ from collections import defaultdict
|
|
|
2
2
|
import numpy as np
|
|
3
3
|
from ortools.sat.python import cp_model
|
|
4
4
|
|
|
5
|
-
from puzzle_solver.core.utils import Pos, get_all_pos, get_char, Direction,
|
|
5
|
+
from puzzle_solver.core.utils import Pos, get_all_pos, get_char, Direction, get_next_pos, get_row_pos, get_col_pos, get_opposite_direction, get_pos
|
|
6
6
|
from puzzle_solver.core.utils_ortools import force_connected_component, generic_solve_all, SingleSolution
|
|
7
7
|
from puzzle_solver.core.utils_visualizer import combined_function
|
|
8
8
|
|
|
File without changes
|
|
File without changes
|