Download src/lib/puzzles/sudoku.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 5.23 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/puzzles/sudoku.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/puzzles/sudoku.ts
-
curl -L -o sudoku.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/puzzles/sudoku.ts
5.23 kB
| /** | |
| * Sudoku puzzle generator and validator. | |
| * Uses backtracking to fill a full solution, then removes cells based on difficulty. | |
| */ | |
| export type SudokuGrid = number[][]; // 9x9, 0 = empty | |
| export type SudokuDifficulty = "easy" | "medium" | "hard"; | |
| // Number of cells revealed per difficulty | |
| const GIVENS: Record<SudokuDifficulty, number> = { | |
| easy: 45, | |
| medium: 35, | |
| hard: 27, | |
| }; | |
| export interface SudokuPuzzle { | |
| puzzle: SudokuGrid; | |
| solution: SudokuGrid; | |
| difficulty: SudokuDifficulty; | |
| } | |
| // ---------- helpers ---------- | |
| function emptyGrid(): SudokuGrid { | |
| return Array.from({ length: 9 }, () => Array(9).fill(0)); | |
| } | |
| function cloneGrid(grid: SudokuGrid): SudokuGrid { | |
| return grid.map((row) => [...row]); | |
| } | |
| function isValidPlacement( | |
| grid: SudokuGrid, | |
| row: number, | |
| col: number, | |
| num: number, | |
| ): boolean { | |
| // Check row | |
| if (grid[row].includes(num)) return false; | |
| // Check column | |
| for (let r = 0; r < 9; r++) { | |
| if (grid[r][col] === num) return false; | |
| } | |
| // Check 3x3 box | |
| const boxRow = Math.floor(row / 3) * 3; | |
| const boxCol = Math.floor(col / 3) * 3; | |
| for (let r = boxRow; r < boxRow + 3; r++) { | |
| for (let c = boxCol; c < boxCol + 3; c++) { | |
| if (grid[r][c] === num) return false; | |
| } | |
| } | |
| return true; | |
| } | |
| /** Shuffle an array in-place using Fisher-Yates. */ | |
| function shuffle<T>(arr: T[]): T[] { | |
| for (let i = arr.length - 1; i > 0; i--) { | |
| const j = Math.floor(Math.random() * (i + 1)); | |
| [arr[i], arr[j]] = [arr[j], arr[i]]; | |
| } | |
| return arr; | |
| } | |
| // ---------- solver / generator ---------- | |
| /** | |
| * Fill the grid with a valid solution via backtracking. | |
| * Numbers are shuffled to produce varied boards. | |
| * Returns true if the grid was fully filled. | |
| */ | |
| function fillGrid(grid: SudokuGrid): boolean { | |
| for (let row = 0; row < 9; row++) { | |
| for (let col = 0; col < 9; col++) { | |
| if (grid[row][col] === 0) { | |
| const nums = shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9]); | |
| for (const num of nums) { | |
| if (isValidPlacement(grid, row, col, num)) { | |
| grid[row][col] = num; | |
| if (fillGrid(grid)) return true; | |
| grid[row][col] = 0; | |
| } | |
| } | |
| return false; // no valid number found → backtrack | |
| } | |
| } | |
| } | |
| return true; // no empty cells left | |
| } | |
| /** | |
| * Count solutions for a given grid. | |
| * Stops after `limit` to keep it fast (we only need to know 0, 1, or 2+). | |
| */ | |
| function countSolutions(grid: SudokuGrid, limit = 2): number { | |
| const g = cloneGrid(grid); | |
| let count = 0; | |
| function solve(): boolean { | |
| for (let row = 0; row < 9; row++) { | |
| for (let col = 0; col < 9; col++) { | |
| if (g[row][col] === 0) { | |
| for (let num = 1; num <= 9; num++) { | |
| if (isValidPlacement(g, row, col, num)) { | |
| g[row][col] = num; | |
| if (solve()) { | |
| if (count >= limit) return true; | |
| } | |
| g[row][col] = 0; | |
| } | |
| } | |
| return false; | |
| } | |
| } | |
| } | |
| count++; | |
| return count >= limit; | |
| } | |
| solve(); | |
| return count; | |
| } | |
| // ---------- public API ---------- | |
| /** | |
| * Generate a Sudoku puzzle with a unique solution. | |
| */ | |
| export function generateSudoku(difficulty: SudokuDifficulty = "medium"): SudokuPuzzle { | |
| // Build complete solution | |
| const solution = emptyGrid(); | |
| fillGrid(solution); | |
| // Build puzzle by removing cells while preserving unique solution | |
| const puzzle = cloneGrid(solution); | |
| const givens = GIVENS[difficulty]; | |
| const totalCells = 81; | |
| const cellsToRemove = totalCells - givens; | |
| // Collect all cell positions and shuffle | |
| const positions: [number, number][] = []; | |
| for (let r = 0; r < 9; r++) { | |
| for (let c = 0; c < 9; c++) { | |
| positions.push([r, c]); | |
| } | |
| } | |
| shuffle(positions); | |
| let removed = 0; | |
| for (const [r, c] of positions) { | |
| if (removed >= cellsToRemove) break; | |
| const backup = puzzle[r][c]; | |
| puzzle[r][c] = 0; | |
| // Ensure unique solution | |
| if (countSolutions(puzzle) !== 1) { | |
| puzzle[r][c] = backup; // restore if it breaks uniqueness | |
| } else { | |
| removed++; | |
| } | |
| } | |
| return { | |
| puzzle, | |
| solution, | |
| difficulty, | |
| }; | |
| } | |
| /** | |
| * Validate a player's completed puzzle against the solution. | |
| * Returns true only if every cell matches. | |
| */ | |
| export function validateSudoku(puzzle: SudokuGrid, solution: SudokuGrid): boolean { | |
| for (let r = 0; r < 9; r++) { | |
| for (let c = 0; c < 9; c++) { | |
| if (puzzle[r][c] !== solution[r][c]) return false; | |
| } | |
| } | |
| return true; | |
| } | |
| /** | |
| * Check if a player's in-progress grid has any obvious rule violations | |
| * (row, col, or box duplicate). Returns a Set of "r,c" strings for invalid cells. | |
| */ | |
| export function findErrors( | |
| playerGrid: SudokuGrid, | |
| givenGrid: SudokuGrid, | |
| ): Set<string> { | |
| const errors = new Set<string>(); | |
| for (let r = 0; r < 9; r++) { | |
| for (let c = 0; c < 9; c++) { | |
| const val = playerGrid[r][c]; | |
| if (val === 0) continue; | |
| if (givenGrid[r][c] !== 0) continue; // given cells can't be errors | |
| // Temporarily clear to check placement | |
| const temp = cloneGrid(playerGrid); | |
| temp[r][c] = 0; | |
| if (!isValidPlacement(temp, r, c, val)) { | |
| errors.add(`${r},${c}`); | |
| } | |
| } | |
| } | |
| return errors; | |
| } | |