-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
267 lines (211 loc) · 8.33 KB
/
data.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
# data.py
import numpy as np
from typing import Tuple, List, Set, Optional
import random
class SudokuDataGenerator:
"""
Generates valid Sudoku puzzles with guaranteed unique solutions.
Includes validation and difficulty rating capabilities.
"""
def __init__(self):
self.size = 9
self.box_size = 3
def generate_puzzle(self, difficulty: str = 'medium') -> Tuple[np.ndarray, np.ndarray]:
"""
Generate a Sudoku puzzle with a unique solution.
Args:
difficulty: 'easy', 'medium', or 'hard'
Returns:
Tuple of (puzzle, solution) as numpy arrays
"""
# Number of cells to remove based on difficulty
cells_to_remove = {
'easy': 30,
'medium': 45,
'hard': 55
}.get(difficulty, 45) # Default to medium if invalid difficulty
solution = self._generate_solved_grid()
puzzle = self._create_puzzle_from_solution(solution, cells_to_remove)
return puzzle, solution
def is_valid(self, grid: np.ndarray, row: int, col: int, num: int) -> bool:
"""
Check if a number is valid in a given position.
Args:
grid: Current Sudoku grid
row: Row index
col: Column index
num: Number to check
Returns:
Boolean indicating if the number is valid
"""
# Check row
if num in grid[row]:
return False
# Check column
if num in grid[:, col]:
return False
# Check 3x3 box
box_row, box_col = 3 * (row // 3), 3 * (col // 3)
for i in range(box_row, box_row + 3):
for j in range(box_col, box_col + 3):
if grid[i, j] == num:
return False
return True
def get_box_indices(self, row: int, col: int) -> List[Tuple[int, int]]:
"""Get all indices in the same 3x3 box as (row, col)."""
box_row, box_col = 3 * (row // 3), 3 * (col // 3)
return [(i, j) for i in range(box_row, box_row + 3)
for j in range(box_col, box_col + 3)]
def get_candidates(self, grid: np.ndarray, row: int, col: int) -> Set[int]:
"""Get all valid candidates for a cell."""
if grid[row, col] != 0:
return set()
candidates = set(range(1, 10))
# Remove numbers from same row
candidates -= set(grid[row])
# Remove numbers from same column
candidates -= set(grid[:, col])
# Remove numbers from same box
box_indices = self.get_box_indices(row, col)
for r, c in box_indices:
candidates.discard(grid[r, c])
return candidates
def _generate_solved_grid(self) -> np.ndarray:
"""Generate a completely solved Sudoku grid."""
grid = np.zeros((9, 9), dtype=int)
def solve(grid: np.ndarray) -> bool:
# Find empty cell
empty = np.where(grid == 0)
if len(empty[0]) == 0:
return True
row, col = empty[0][0], empty[1][0]
# Try digits in random order
for num in random.sample(range(1, 10), 9):
if self.is_valid(grid, row, col, num):
grid[row, col] = num
if solve(grid):
return True
grid[row, col] = 0
return False
solve(grid)
return grid
def _create_puzzle_from_solution(self, solution: np.ndarray, cells_to_remove: int) -> np.ndarray:
"""
Create a puzzle by removing numbers from a solved grid,
ensuring a unique solution remains.
"""
puzzle = solution.copy()
positions = list(np.ndindex(9, 9))
random.shuffle(positions)
removed = 0
for pos in positions:
if removed >= cells_to_remove:
break
# Try removing the number
backup = puzzle[pos]
puzzle[pos] = 0
# Check if solution is still unique
if not self._has_unique_solution(puzzle):
puzzle[pos] = backup
else:
removed += 1
return puzzle
def _has_unique_solution(self, grid: np.ndarray) -> bool:
"""Check if a puzzle has exactly one solution."""
solutions_found = []
def count_solutions(g: np.ndarray) -> None:
if len(solutions_found) > 1:
return
if not np.any(g == 0):
solutions_found.append(g.copy())
return
# Find first empty cell
empty = np.where(g == 0)
row, col = empty[0][0], empty[1][0]
# Try each possible number
for num in range(1, 10):
if self.is_valid(g, row, col, num):
g[row, col] = num
count_solutions(g)
g[row, col] = 0
return
grid_copy = grid.copy()
count_solutions(grid_copy)
return len(solutions_found) == 1
def rate_difficulty(self, puzzle: np.ndarray) -> str:
"""
Rate the difficulty of a puzzle based on:
1. Number of empty cells
2. Distribution of given numbers
3. Required solving techniques
Returns: 'easy', 'medium', or 'hard'
"""
# Count empty cells
empty_count = np.sum(puzzle == 0)
if empty_count < 35:
return 'easy'
elif empty_count > 50:
return 'hard'
# Check distribution of givens in rows/columns/boxes
row_counts = [np.sum(puzzle[i] != 0) for i in range(9)]
col_counts = [np.sum(puzzle[:, i] != 0) for i in range(9)]
# Calculate standard deviation of givens distribution
std_dev = np.std(row_counts + col_counts)
if std_dev < 1.0: # Even distribution
return 'easy'
elif std_dev > 2.0: # Uneven distribution
return 'hard'
return 'medium'
def validate_puzzle(self, puzzle: np.ndarray) -> bool:
"""
Validate that a puzzle follows Sudoku rules:
- No duplicates in rows
- No duplicates in columns
- No duplicates in boxes
- Has valid dimensions
"""
if puzzle.shape != (9, 9):
return False
# Check each row
for i in range(9):
row = puzzle[i][puzzle[i] != 0]
if len(set(row)) != len(row):
return False
# Check each column
for j in range(9):
col = puzzle[:, j][puzzle[:, j] != 0]
if len(set(col)) != len(col):
return False
# Check each box
for box_row in range(3):
for box_col in range(3):
box = puzzle[box_row*3:(box_row+1)*3,
box_col*3:(box_col+1)*3]
box_vals = box[box != 0]
if len(set(box_vals)) != len(box_vals):
return False
return True
def load_puzzle_from_file(filename: str) -> Optional[np.ndarray]:
"""
Load a puzzle from a file. File should contain 9 lines with 9 numbers each.
Use 0 for empty cells.
"""
try:
puzzle = np.loadtxt(filename, dtype=int)
if puzzle.shape != (9, 9):
raise ValueError("Invalid puzzle dimensions")
generator = SudokuDataGenerator()
if not generator.validate_puzzle(puzzle):
raise ValueError("Invalid puzzle: breaks Sudoku rules")
return puzzle
except Exception as e:
print(f"Error loading puzzle: {e}")
return None
def save_puzzle_to_file(puzzle: np.ndarray, filename: str) -> bool:
"""Save a puzzle to a file."""
try:
np.savetxt(filename, puzzle, fmt='%d')
return True
except Exception as e:
print(f"Error saving puzzle: {e}")
return False