-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
103 lines (94 loc) · 2.33 KB
/
main.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
function Point() {
this.suspects = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
this.refresh0 = false;
}
Point.prototype.set = function(value) {
if (this.ok()) {
return false;
}
this.suspects = [ value ];
return true;
}
Point.prototype.exclude = function(value) {
if (this.ok()) {
return false;
}
for (let x = 0; x < this.suspects.length; x++) {
if (this.suspects[x] === value) {
this.suspects.splice(x, 1);
return this.ok();
}
}
return false;
}
Point.prototype.ok = function() {
return this.suspects.length == 1;
}
function Sudoku(mat) {
this.count = 0;
this.points = [];
for (let x = 0; x < 9; x++) {
let line = [];
this.points.push(line);
for (let y = 0; y < 9; y++) {
let point = new Point();
if (mat[x][y] > 0) {
this.count += point.set(mat[x][y]) ? 1 : 0;
}
line.push(point);
}
}
}
Sudoku.prototype.print = function() {
for (let x = 0; x < 9; x++) {
for (let y = 0; y < 9; y++) {
console.log(x + 1, y + 1, this.points[x][y].suspects);
}
}
}
Sudoku.prototype.go = function() {
this.refresh0();
this.print();
}
Sudoku.prototype.refresh0 = function() {
while (true) {
let c = this.count;
for (let x = 0; x < 9; x++) {
for (let y = 0; y < 9; y++) {
let p = this.points[x][y];
if (p.ok() && !p.refresh0) {
// exclude same line
let v = p.suspects[0];
for (let k = 0; k < 9; k++) {
this.count += this.points[k][y].exclude(v) ? 1 : 0;
this.count += this.points[x][k].exclude(v) ? 1 : 0;
}
// exclude same palace
let px = Math.floor(x / 3) * 3;
let py = Math.floor(y / 3) * 3;
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 2; j++) {
this.count += this.points[px + i][py + j].exclude(v) ? 1 : 0;
}
}
}
}
}
if (c === this.count) {
break;
}
}
return this.count === 81;
}
const sudoku = new Sudoku([
[ 0, 0, 0, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 2, 7, 0, 0, 0 ],
[ 4, 0, 6, 0, 0, 0, 1, 0, 9 ],
[ 0, 0, 0, 0, 0, 0, 9, 2, 5 ],
[ 6, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 3, 0, 0, 8, 1, 0, 0, 0, 0 ],
[ 7, 0, 0, 0, 4, 0, 3, 0, 0 ],
[ 0, 0, 4, 0, 0, 0, 0, 0, 7 ],
[ 0, 8, 5, 0, 9, 0, 0, 0, 4 ] ]);
sudoku.go();
console.log(sudoku.count);