UNPKG

sudoku-solve

Version:

Konni's Sudoku solving library

76 lines (75 loc) 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupType = exports.Group = void 0; const CommonFunctions_1 = require("./CommonFunctions"); const NumberArray_1 = require("./NumberArray"); const NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var GroupType; (function (GroupType) { GroupType["ROW"] = "ROW"; GroupType["COL"] = "COL"; GroupType["BOX"] = "BOX"; })(GroupType || (GroupType = {})); exports.GroupType = GroupType; class Group { constructor(type, index, numbers) { this.type = type; this.index = index; this.numbers = numbers; } static group(type, index, allNumbers) { if (GroupType.ROW === type) { const numbers = allNumbers.filter((n, i) => index === CommonFunctions_1.row(i)); return new Group(type, index, numbers); } else if (GroupType.COL === type) { const numbers = allNumbers.filter((n, i) => index === CommonFunctions_1.col(i)); return new Group(type, index, numbers); } else { const numbers = allNumbers.filter((n, i) => index === CommonFunctions_1.box(i)); return new Group(type, index, numbers); } } static numbers(type, index, allNumbers) { if (GroupType.ROW === type) { return allNumbers.filter((n, i) => index === CommonFunctions_1.row(i)); } else if (GroupType.COL === type) { return allNumbers.filter((n, i) => index === CommonFunctions_1.col(i)); } else { return allNumbers.filter((n, i) => index === CommonFunctions_1.box(i)); } } static indices(type, index) { if (GroupType.ROW === type) { return Array(81).fill(0).map((n, i) => i).filter((i) => index === CommonFunctions_1.row(i)); } else if (GroupType.COL === type) { return Array(81).fill(0).map((n, i) => i).filter((i) => index === CommonFunctions_1.col(i)); } else { return Array(81).fill(0).map((n, i) => i).filter((i) => index === CommonFunctions_1.box(i)); } } getType() { return this.type; } isType(type) { return this.type === type; } knownNumbers() { return this.numbers.filter((n) => n.isSolved()).map((n) => n.get()); } unknownNumbers() { return NUMBERS.filter(n => !NumberArray_1.NumberArray.contains(this.knownNumbers(), n)); } isCorrect() { const knownNumbers = new NumberArray_1.NumberArray(this.knownNumbers()); return this.knownNumbers() .map(n => knownNumbers.retain([n]).length > 1) .every(isDuplicate => isDuplicate === false); } } exports.Group = Group;