-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku-utils.js
67 lines (58 loc) · 1.91 KB
/
sudoku-utils.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Module from "./main.js"
const N = 9
const matrix = (rows, cols) => new Array(cols).fill(0).map((o, i) => new Array(rows).fill(0))
const getSudokuGrid = () => {
let sudokuMatrix = matrix(9, 9);
$('.form-control').each(function(i, obj) {
sudokuMatrix[obj.id.charAt(0)][obj.id.charAt(1)] = $(obj).val() == "" ? 0 : parseInt($(obj).val());
});
return sudokuMatrix;
}
const setSudokuGrid = (resultMatrix) => {
$('.form-control').each(function(i, obj) {
$(obj).val(resultMatrix[obj.id.charAt(0)][obj.id.charAt(1)])
})
}
const resetSudokuGrid = () => {
$('.form-control').each(function(i, obj) {
$(obj).val("");
})
}
const makePtrOfArray = (myModule) => {
let sudokuMatrix = getSudokuGrid();
const arrayPtr = myModule._calloc(N, 4);
for (let i = 0; i < N; i++) {
let rowsPtr = myModule._calloc(N, 4);
myModule.setValue(arrayPtr + i * 4, rowsPtr, "i32");
for (let j = 0; j < N; j++) {
myModule.setValue(rowsPtr + j * 4, sudokuMatrix[i][j], "i32");
}
}
return arrayPtr;
}
const getArrayFromPtr = (myModule, ptr) => {
let resultMatrix = matrix(9, 9);
for (let i = 0; i < N; i++) {
let rowsPtr = myModule.getValue(ptr + i * 4, "i32");
for (let j = 0; j < N; j++) {
resultMatrix[i][j] = myModule.getValue(rowsPtr + j * 4, "i32");
}
}
return resultMatrix;
}
const resetBtn = document.getElementById("reset-btn");
resetBtn.onclick = () => {
resetSudokuGrid();
}
Module().then(function (mymod) {
let solveBtn = document.getElementById("solve-btn");
solveBtn.onclick = () => {
let arrPtr = makePtrOfArray(mymod);
let startDate = window.performance.now();
let solverResult = mymod._SolveSudoku(arrPtr);
let endDate = window.performance.now();
let resultMatrix = getArrayFromPtr(mymod, arrPtr);
setSudokuGrid(resultMatrix);
alert(`${solverResult ? 'Solved!': 'No results found'} Excecution time: ${(endDate - startDate)} ms`);
}
})