-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (27 loc) · 1.06 KB
/
index.ts
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
import { SudokuCell, generateConstraints, parseStringFormat } from './lib'
import { findRaw } from 'dancing-links'
const FIELD_SIZE = 9
export function solveString (sudoku: string, all = false): SudokuCell[][] {
const totalConstraints = (FIELD_SIZE * FIELD_SIZE) * 4
const cells = parseStringFormat(sudoku, FIELD_SIZE)
const constraints = generateConstraints(cells, FIELD_SIZE)
const result = findRaw({
numPrimary: totalConstraints,
numSecondary: 0,
numSolutions: all ? Infinity : 1,
rows: constraints
})
return result.map((r) => r.map((s) => s.data))
}
export function solveCells (sudoku: SudokuCell[], all = false): SudokuCell[][] {
const totalConstraints = (FIELD_SIZE * FIELD_SIZE) * 4
const constraints = generateConstraints(sudoku, FIELD_SIZE)
const result = findRaw({
numPrimary: totalConstraints,
numSecondary: 0,
numSolutions: all ? Infinity : 1,
rows: constraints
})
return result.map((r) => r.map((s) => s.data))
}
export { SudokuCell, parseStringFormat, generateConstraints, printBoard } from './lib/index'