1
var solveSudoku = function (board) {
2
let intBoard = convertBoardToInteger(board);
3

4
let y = 0,
5
x = 0;
6

7
while (y <= 8) {
8
x = 0;
9
while (x <= 8) {
10
if (isFreeIndex(board, x, y)) {
11
intBoard[y][x]++;
12
while (!isPossibleValue(intBoard, x, y) && intBoard[y][x] < 10) {
13
intBoard[y][x]++;
14
}
15

16
if (intBoard[y][x] > 9) {
17
intBoard[y][x] = 0;
18
let a = retraceBoard(board, x, y);
19
(x = a[0]), (y = a[1]);
20
x--;
21
}
22
}
23
x++;
24
}
25
y++;
26
}
27

28
convertIntBoardToString(intBoard, board);
29
};
30

31
var isFreeIndex = function (board, x, y) {
32
return board[y][x] == ".";
33
};
34

35
var isPossibleValue = function (intBoard, x, y) {
36
return (
37
isUniqueInColumn(intBoard, x, y) &&
38
isUniqueOnRow(intBoard, x, y) &&
39
isUniqueInBox(intBoard, x, y)
40
);
41
};
42
var isUniqueOnRow = function (intBoard, x, y) {
43
for (let i = 0; i < 9; i++) {
44
if (intBoard[i][x] == intBoard[y][x] && i != y) {
45
return false;
46
}
47
}
48
return true;
49
};
50
var isUniqueInColumn = function (intBoard, x, y) {
51
for (let i = 0; i < 9; i++) {
52
if (intBoard[y][i] == intBoard[y][x] && i != x) {
53
return false;
54
}
55
}
56
return true;
57
};
58
var isUniqueInBox = function (intBoard, x, y) {
59
let startX = x - (x % 3);
60
let startY = y - (y % 3);
61

62
for (let i = startX; i < startX + 3; i++) {
63
for (let j = startY; j < startY + 3; j++) {
64
if (intBoard[j][i] == intBoard[y][x] && (i != x || j != y)) {
65
return false;
66
}
67
}
68
}
69
return true;
70
};
71

72
var retraceBoard = function (board, x, y) {
73
do {
74
if (x > 0) {
75
x--;
76
} else {
77
x = 8;
78
y--;
79
}
80
} while (board[y][x] != ".");
81
return [x, y];
82
};
83

84
var convertBoardToInteger = function (board) {
85
let intBoard = new Array(8);
86
for (let y = 0; y < 9; y++) {
87
intBoard[y] = new Array(8);
88
for (let x = 0; x < 9; x++) {
89
intBoard[y][x] = parseInt(board[y][x]);
90
if (!intBoard[y][x]) intBoard[y][x] = 0;
91
}
92
}
93
return intBoard;
94
};
95
var convertIntBoardToString = function (intBoard, board) {
96
for (let y = 0; y < 9; y++) {
97
for (let x = 0; x < 9; x++) {
98
board[y][x] = intBoard[y][x].toString();
99
}
100
}
101

102
return board;
103
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0