[HAI] isValidSudoku WIP
parent
6c6bb69160
commit
9e79211174
@ -0,0 +1,3 @@
|
|||||||
|
import { isValidSudoku } from "./isValidSudoku";
|
||||||
|
|
||||||
|
export default isValidSudoku;
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { isValidSudoku } from "./isValidSudoku.ts";
|
||||||
|
|
||||||
|
describe("isValidSudoku", () => {
|
||||||
|
|
||||||
|
it("true case", () => {
|
||||||
|
const board =
|
||||||
|
[["5", "3", ".", ".", "7", ".", ".", ".", "."]
|
||||||
|
, ["6", ".", ".", "1", "9", "5", ".", ".", "."]
|
||||||
|
, [".", "9", "8", ".", ".", ".", ".", "6", "."]
|
||||||
|
, ["8", ".", ".", ".", "6", ".", ".", ".", "3"]
|
||||||
|
, ["4", ".", ".", "8", ".", "3", ".", ".", "1"]
|
||||||
|
, ["7", ".", ".", ".", "2", ".", ".", ".", "6"]
|
||||||
|
, [".", "6", ".", ".", ".", ".", "2", "8", "."]
|
||||||
|
, [".", ".", ".", "4", "1", "9", ".", ".", "5"]
|
||||||
|
, [".", ".", ".", ".", "8", ".", ".", "7", "9"]];
|
||||||
|
|
||||||
|
expect(isValidSudoku(board)).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("false case", () => {
|
||||||
|
const board =
|
||||||
|
[["8", "3", ".", ".", "7", ".", ".", ".", "."]
|
||||||
|
, ["6", ".", ".", "1", "9", "5", ".", ".", "."]
|
||||||
|
, [".", "9", "8", ".", ".", ".", ".", "6", "."]
|
||||||
|
, ["8", ".", ".", ".", "6", ".", ".", ".", "3"]
|
||||||
|
, ["4", ".", ".", "8", ".", "3", ".", ".", "1"]
|
||||||
|
, ["7", ".", ".", ".", "2", ".", ".", ".", "6"]
|
||||||
|
, [".", "6", ".", ".", ".", ".", "2", "8", "."]
|
||||||
|
, [".", ".", ".", "4", "1", "9", ".", ".", "5"]
|
||||||
|
, [".", ".", ".", ".", "8", ".", ".", "7", "9"]]
|
||||||
|
expect(isValidSudoku(board)).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
export function isValidSudoku(board: string[][]): boolean {
|
||||||
|
const rowTable: Record<string, string[]> = {}
|
||||||
|
const colTable: Record<string, string[]> = {}
|
||||||
|
const cellTable: Record<string, string[]> = {}
|
||||||
|
|
||||||
|
let cellNum = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < board.length; i++) {
|
||||||
|
if (i + 1 % 3 === 0) cellNum = i;
|
||||||
|
|
||||||
|
for (let j = 0; j < board[i].length; j++) {
|
||||||
|
// check repeat in rows
|
||||||
|
const rowValue = board[i][j];
|
||||||
|
const rowKey = i + "_" + cellNum;
|
||||||
|
if (rowValue === ".") continue;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!rowTable[rowKey]
|
||||||
|
) {
|
||||||
|
rowTable[rowKey] = [rowValue];
|
||||||
|
} else {
|
||||||
|
if (rowTable[rowKey].includes(rowValue)) return false;
|
||||||
|
rowTable[i + "_" + cellNum].push(rowValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check repeat in collumns
|
||||||
|
const collValue = board[i][i];
|
||||||
|
const collKey = i + "_" + cellNum;
|
||||||
|
|
||||||
|
if (collValue === ".") continue;
|
||||||
|
|
||||||
|
if (!colTable[collKey]) {
|
||||||
|
colTable[collKey] = [collValue];
|
||||||
|
} else {
|
||||||
|
if (colTable[collKey] && colTable[collKey].includes(collValue)) return false;
|
||||||
|
colTable[collKey].push(collValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create cellTable
|
||||||
|
for (let cellKey = 0; cellKey < board.length; cellKey++) {
|
||||||
|
if (cellKey + 1 % 3 === 0) cellNum = (cellKey + 1) / 3;
|
||||||
|
let rowKey = cellKey + "_" + cellNum;
|
||||||
|
let rowValues = rowTable[rowKey];
|
||||||
|
|
||||||
|
if (!cellTable[cellKey]) {
|
||||||
|
cellTable[cellKey] = rowValues;
|
||||||
|
} else {
|
||||||
|
rowValues.forEach(value => {
|
||||||
|
if (
|
||||||
|
cellTable[cellKey].includes(value)
|
||||||
|
) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
cellTable[cellKey].concat(rowValues);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log({ cellTable, rowTable, colTable })
|
||||||
|
|
||||||
|
|
||||||
|
return true
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue