Skip to content

Commit 3c5b67a

Browse files
Add visualization and example to main.py
1 parent 003d257 commit 3c5b67a

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-10
lines changed

main.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
import time
33

44

5+
def example_8x8():
6+
"""8x8 example from https://puzzlegenius.org/snake/"""
7+
puzzle = SnakePuzzle(
8+
row_sums=[4, 2, 2, 3, 1, 3, 2, 6],
9+
col_sums=[3, 2, 7, 2, 2, 4, 1, 2],
10+
start_cell=(2, 5),
11+
end_cell=(6, 7)
12+
)
13+
return puzzle
14+
15+
516
def main():
617
"""
718
Example usage of the Snake solver.
819
9-
TODO: Replace with your specific puzzle parameters and usage.
1020
"""
1121
print("Snake MIP Solver - Example Usage")
1222
print("=" * 50)
1323

14-
# TODO: Create your puzzle instance
15-
# Example:
16-
# puzzle = SnakePuzzle(
17-
# size=5,
18-
# constraints=[1, 2, 3, 2, 1]
19-
# )
20-
24+
2125
try:
22-
# For now, create a basic puzzle (replace with your parameters)
23-
puzzle = SnakePuzzle()
26+
puzzle = example_8x8()
2427
print(f"Created puzzle: {puzzle}")
2528

2629
# Create solver
@@ -37,7 +40,34 @@ def main():
3740
if solution is not None:
3841
print(f"\n✅ Solution found in {solve_time:.3f} seconds!")
3942
print(f"Solution: {solution}")
43+
print(puzzle.get_board_visualization())
4044

45+
manual_solution = set()
46+
manual_solution.add((2, 5))
47+
manual_solution.add((1, 5))
48+
manual_solution.add((0, 5))
49+
manual_solution.add((0, 4))
50+
manual_solution.add((0, 3))
51+
manual_solution.add((0, 2))
52+
manual_solution.add((1, 2))
53+
manual_solution.add((2, 2))
54+
manual_solution.add((3, 2))
55+
manual_solution.add((3, 1))
56+
manual_solution.add((3, 0))
57+
manual_solution.add((4, 0))
58+
manual_solution.add((5, 0))
59+
manual_solution.add((5, 1))
60+
manual_solution.add((5, 2))
61+
manual_solution.add((6, 2))
62+
manual_solution.add((7, 2))
63+
manual_solution.add((7, 3))
64+
manual_solution.add((7, 4))
65+
manual_solution.add((7, 5))
66+
manual_solution.add((7, 6))
67+
manual_solution.add((7, 7))
68+
manual_solution.add((6, 7))
69+
print(puzzle.get_board_visualization(manual_solution, show_indices=True))
70+
print(f"Manual solution valid? {puzzle.is_valid_solution(manual_solution)}")
4171
# TODO: Add solution visualization or validation
4272
# Example:
4373
# if puzzle.is_valid_solution(solution):

snake_mip_solver/puzzle.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,53 @@ def get_start_cell(self) -> Tuple[int, int]:
273273
def get_end_cell(self) -> Tuple[int, int]:
274274
"""Get the end cell position."""
275275
return self.end_cell
276+
277+
def get_board_visualization(self, snake_positions: Optional[Set[Tuple[int, int]]] = None,
278+
show_indices: bool = False) -> str:
279+
"""
280+
Create a string representation of the puzzle.
281+
282+
Args:
283+
snake_positions: Optional set of snake body positions to display
284+
show_indices: Whether to show row/column indices
285+
286+
Returns:
287+
String representation of the puzzle
288+
"""
289+
snake_positions = snake_positions or set()
290+
lines = []
291+
292+
# Header with column indices and sums
293+
if show_indices:
294+
header = " " + " ".join(str(i) for i in range(self.cols))
295+
lines.append(header)
296+
col_sums_line = " " + " ".join(str(s) if s is not None else "?" for s in self.col_sums)
297+
else:
298+
col_sums_line = " " + " ".join(str(s) if s is not None else "?" for s in self.col_sums)
299+
300+
lines.append(col_sums_line)
301+
302+
# Board rows
303+
for row in range(self.rows):
304+
row_parts = []
305+
306+
if show_indices:
307+
row_parts.append(str(row))
308+
309+
row_sum_str = str(self.row_sums[row]) if self.row_sums[row] is not None else "?"
310+
row_parts.append(row_sum_str)
311+
312+
for col in range(self.cols):
313+
pos = (row, col)
314+
if pos == self.start_cell:
315+
row_parts.append('S')
316+
elif pos == self.end_cell:
317+
row_parts.append('E')
318+
elif pos in snake_positions:
319+
row_parts.append('x') # █
320+
else:
321+
row_parts.append('_')
322+
323+
lines.append(' '.join(row_parts))
324+
325+
return '\n'.join(lines)

0 commit comments

Comments
 (0)