aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/sudoku/validation.ts
diff options
context:
space:
mode:
authorMaksymilian Jopek <maks@jopek.eu>2022-12-19 21:04:23 +0100
committerMaksymilian Jopek <maks@jopek.eu>2022-12-19 21:04:23 +0100
commit44ff6231441024d16fddc2b7cb648539522b04cb (patch)
tree8302d0734f4ce931631ffe6d43af5b9a5da284a0 /src/lib/sudoku/validation.ts
parent9f4618c5ef618521ce1ab680d82ed9cc941d10d8 (diff)
downloaddigit-single-44ff6231441024d16fddc2b7cb648539522b04cb.tar.gz
digit-single-44ff6231441024d16fddc2b7cb648539522b04cb.tar.zst
digit-single-44ff6231441024d16fddc2b7cb648539522b04cb.zip
v1.0.0
Added everything that's in the spec
Diffstat (limited to 'src/lib/sudoku/validation.ts')
-rw-r--r--src/lib/sudoku/validation.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/lib/sudoku/validation.ts b/src/lib/sudoku/validation.ts
new file mode 100644
index 0000000..4403a20
--- /dev/null
+++ b/src/lib/sudoku/validation.ts
@@ -0,0 +1,57 @@
+import type { Coord, Grid } from "../Sudoku";
+
+export function validate(board: Grid) {
+ const bads = [] as Coord[];
+ for (let i = 0; i < 9; i++) {
+ for (let j = 0; j < 9; j++) {
+ const value = board[i][j];
+ if (value !== '.') {
+ if (!validateRow(board, i, j, value) || !validateColumn(board, i, j, value) || !validateBox(board, i, j, value)) {
+ bads.push({ x: i, y: j })
+ }
+ }
+ }
+ }
+ return bads;
+};
+
+function validateRow(board: Grid, row: number, col: number, value: string) {
+ for (let j = 0; j < 9; j++) {
+ if (j !== col) {
+ if (board[row][j] === value) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+function validateColumn(board: Grid, row: number, col: number, value: string) {
+ for (let i = 0; i < 9; i++) {
+ if (i !== row) {
+ if (board[i][col] === value) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+function validateBox(board: Grid, row: number, col: number, value: string) {
+ const startRow = row - (row % 3), startCol = col - (col % 3);
+
+ for (let i = startRow; i < startRow + 3; i++) {
+ for (let j = startCol; j < startCol + 3; j++) {
+ if (i !== row && j !== col) {
+ if (board[i][j] === value) {
+ return false;
+ }
+ }
+ }
+ }
+
+ return true;
+}
+