-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolutionValidator.ts
More file actions
43 lines (40 loc) · 1.28 KB
/
SudokuSolutionValidator.ts
File metadata and controls
43 lines (40 loc) · 1.28 KB
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
36
37
38
39
40
41
42
43
// https://www.codewars.com/kata/529bf0e9bdf7657179000008/solutions/javascript
export function validSolution(board: number[][]) {
let rows = board;
let columns = [];
for (let i = 0; i < 9; i++) {
const row = board[i];
const column: number[] = [];
columns.push(column);
for (let j = 0; j < 9; j++) {
column.push(rows[j][i]);
}
}
let blocks = [];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
const block: number[] = [];
blocks.push(block);
for (let m = 0; m < 3; m++) {
for (let n = 0; n < 3; n++) {
let x = i * 3 + m;
let y = j * 3 + n;
block.push(rows[x][y])
}
}
}
}
let noZero = checkNoZero(rows);
let noDuplicate = checkNoDuplicate(rows) && checkNoDuplicate(columns) && checkNoDuplicate(blocks);
return noZero && noDuplicate;
}
function checkNoDuplicate(array: number[][]) {
return array
.map(element => new Set(element).size == 9)
.reduce((a, b) => a && b);
}
function checkNoZero(array: number[][]) {
return array
.map(element => element.filter(e => e == 0).length == 0)
.reduce((a, b) => a && b);
}