From 0a9d8b23a2cc167ecff8480a85f767a7ec913bf4 Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Wed, 27 Nov 2024 23:36:30 +0300 Subject: [PATCH] [HAI] isValidSudoku WIP --- lib/isValidSudoku/isValidSudoku.test.ts | 4 +- lib/isValidSudoku/isValidSudoku.ts | 56 ++----------------------- 2 files changed, 6 insertions(+), 54 deletions(-) diff --git a/lib/isValidSudoku/isValidSudoku.test.ts b/lib/isValidSudoku/isValidSudoku.test.ts index d5d8e91..d2bea08 100644 --- a/lib/isValidSudoku/isValidSudoku.test.ts +++ b/lib/isValidSudoku/isValidSudoku.test.ts @@ -4,7 +4,7 @@ import { isValidSudoku } from "./isValidSudoku.ts"; describe("isValidSudoku", () => { it("true case", () => { - const board = + let board = [["5", "3", ".", ".", "7", ".", ".", ".", "."] , ["6", ".", ".", "1", "9", "5", ".", ".", "."] , [".", "9", "8", ".", ".", ".", ".", "6", "."] @@ -19,7 +19,7 @@ describe("isValidSudoku", () => { }); it("false case", () => { - const board = + let board = [["8", "3", ".", ".", "7", ".", ".", ".", "."] , ["6", ".", ".", "1", "9", "5", ".", ".", "."] , [".", "9", "8", ".", ".", ".", ".", "6", "."] diff --git a/lib/isValidSudoku/isValidSudoku.ts b/lib/isValidSudoku/isValidSudoku.ts index 189dedb..bf68845 100644 --- a/lib/isValidSudoku/isValidSudoku.ts +++ b/lib/isValidSudoku/isValidSudoku.ts @@ -3,63 +3,15 @@ export function isValidSudoku(board: string[][]): boolean { const colTable: Record = {} const cellTable: Record = {} - 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) - } + rowTable[i] = board[i].filter(item => item !== "."); + if (new Set(rowTable[i]).size !== rowTable[i].length) return false; + colTable[i] = board.map(item => item[i]).filter(item => item !== "."); + if (new Set(colTable[i]).size !== rowTable[i].length) return false; } - // 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 };