-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.js
161 lines (142 loc) · 4.5 KB
/
sudoku.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use strict";
var TESTABLE = true;
var SudokuSolver = function (testable) {
var solver;
function solve(boardString) {
var boardArray = boardString.split("");
if (boardIsInvalid(boardArray)) {
return false;
}
return recursiveSolve(boardString);
}
function solveAndPrint(boardString) {
var solvedBoard = solve(boardString);
console.log(toString(solvedBoard.split("")));
return solvedBoard;
}
function recursiveSolve(boardString) {
var boardArray = boardString.split("");
if (boardIsSolved(boardArray)) {
return boardArray.join("");
}
var cellPossibilities = getNextCellAndPossibilities(boardArray);
var nextUnsolvedCellIndex = cellPossibilities.index;
var possibilities = cellPossibilities.choices;
for (var i = 0; i < possibilities.length; i++) {
boardArray[nextUnsolvedCellIndex] = possibilities[i];
var solvedBoard = recursiveSolve(boardArray.join(""));
if (solvedBoard) {
return solvedBoard;
}
}
return false;
}
function boardIsInvalid(boardArray) {
return !boardIsValid(boardArray);
}
function boardIsValid(boardArray) {
return allRowsValid(boardArray) && allColumnsValid(boardArray) && allBoxesValid(boardArray);
}
function boardIsSolved(boardArray) {
for (var i = 0; i < boardArray.length; i++) {
if (boardArray[i] === "-") {
return false;
}
}
return true;
}
function getNextCellAndPossibilities(boardArray) {
for (var i = 0; i < boardArray.length; i++) {
if (boardArray[i] === "-") {
var existingValues = getAllIntersections(boardArray, i);
var choices = ["1", "2", "3", "4", "5", "6", "7", "8", "9"].filter(function (num) {
return existingValues.indexOf(num) < 0;
});
return { index: i, choices: choices };
}
}
}
function getAllIntersections(boardArray, i) {
return getRow(boardArray, i).concat(getColumn(boardArray, i)).concat(getBox(boardArray, i));
}
function allRowsValid(boardArray) {
return [0, 9, 18, 27, 36, 45, 54, 63, 72].map(function (i) {
return getRow(boardArray, i);
}).reduce(function (validity, row) {
return collectionIsValid(row) && validity;
}, true);
}
function getRow(boardArray, i) {
var startingEl = Math.floor(i / 9) * 9;
return boardArray.slice(startingEl, startingEl + 9);
}
function allColumnsValid(boardArray) {
return [0, 1, 2, 3, 4, 5, 6, 7, 8].map(function (i) {
return getColumn(boardArray, i);
}).reduce(function (validity, row) {
return collectionIsValid(row) && validity;
}, true);
}
function getColumn(boardArray, i) {
var startingEl = Math.floor(i % 9);
return [0, 1, 2, 3, 4, 5, 6, 7, 8].map(function (num) {
return boardArray[startingEl + num * 9];
});
}
function allBoxesValid(boardArray) {
return [0, 3, 6, 27, 30, 33, 54, 57, 60].map(function (i) {
return getBox(boardArray, i);
}).reduce(function (validity, row) {
return collectionIsValid(row) && validity;
}, true);
}
function getBox(boardArray, i) {
var boxCol = Math.floor(i / 3) % 3;
var boxRow = Math.floor(i / 27);
var startingIndex = boxCol * 3 + boxRow * 27;
return [0, 1, 2, 9, 10, 11, 18, 19, 20].map(function (num) {
return boardArray[startingIndex + num];
});
}
function collectionIsValid(collection) {
var numCounts = {};
for(var i = 0; i < collection.length; i++) {
if (collection[i] != "-") {
if (numCounts[collection[i]] === undefined) {
numCounts[collection[i]] = 1;
} else {
return false;
}
}
}
return true;
}
function toString(boardArray) {
return [0, 9, 18, 27, 36, 45, 54, 63, 72].map(function (i) {
return getRow(boardArray, i).join(" ");
}).join("\n");
}
if (testable) {
solver = {
solve: solve,
solveAndPrint: solveAndPrint,
recursiveSolve: recursiveSolve,
boardIsInvalid: boardIsInvalid,
boardIsValid: boardIsValid,
boardIsSolved: boardIsSolved,
getNextCellAndPossibilities: getNextCellAndPossibilities,
getAllIntersections: getAllIntersections,
allRowsValid: allRowsValid,
getRow: getRow,
allColumnsValid: allColumnsValid,
getColumn: getColumn,
allBoxesValid: allBoxesValid,
getBox: getBox,
collectionIsValid: collectionIsValid,
toString: toString };
} else {
solver = { solve: solve,
solveAndPrint: solveAndPrint };
}
return solver;
}(TESTABLE);