-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (115 loc) · 3.18 KB
/
index.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
/* jshint node:true */
'use strict';
var _ = require('lodash');
var buildColumns = module.exports.buildColumns = function(cells) {
var i,
j,
matrix = [[], [], [], [], [], [], [], [], []];
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
matrix[j][i] = cells[(i * 9) + j];
}
}
return matrix;
};
var buildRows = module.exports.buildRows = function(cells) {
var i,
j,
matrix = [[], [], [], [], [], [], [], [], []];
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
matrix[i][j] = cells[(i * 9) + j];
}
}
return matrix;
};
var buildGroups = module.exports.buildGroups = function(cells) {
var group,
groupPosition,
row,
column,
startRow,
startColumn,
matrix = [[], [], [], [], [], [], [], [], []];
for (group = 0; group < 9; group++) {
startRow = Math.floor(group/3) * 3;
groupPosition = 0;
for (row = startRow; row < startRow + 3; row++) {
startColumn = (group % 3) * 3;
for (column = startColumn; column < startColumn + 3; column++) {
matrix[group][groupPosition] = cells[(row * 9) + column];
groupPosition++;
}
}
}
return matrix;
};
var Cell = module.exports.Cell = function Cell(initial) {
this.row = null;
this.column = null;
this.group = null;
this.value = null;
this.solved = false;
if (isNaN(Number(initial))) {
this.possibilities = _.range(1, 10);
} else {
this.possibilities = [Number(initial)];
}
this.checkForSolved();
};
Cell.prototype.checkForSolved = function() {
if (this.possibilities.length === 1) {
this.solved = true;
this.value = this.possibilities[0];
}
};
Cell.prototype.applyRestrictions = function() {
var changedAnything = false;
if (! this.solved) {
['row', 'column', 'group'].forEach(function(grouping) {
this[grouping].forEach(function(otherCell) {
if (otherCell.solved) {
if (this.possibilities.indexOf(otherCell.value) !== -1) {
this.possibilities.splice(this.possibilities.indexOf(otherCell.value), 1);
this.checkForSolved();
changedAnything = true;
}
}
}.bind(this));
}.bind(this));
}
return changedAnything;
};
var populateReferences = module.exports.populateReferences = function populateReferences(cells, rows, columns, groups) {
rows.forEach(function (row) {
row.forEach(function(cell) {
cell.row = row;
});
});
columns.forEach(function (column) {
column.forEach(function(cell) {
cell.column = column;
});
});
groups.forEach(function (group) {
group.forEach(function(cell) {
cell.group = group;
});
});
};
module.exports.solve = function(board) {
var cells = board.split('').map(function(initial) {return new Cell(initial);});
var rows = buildRows(cells);
var columns = buildColumns(cells);
var groups = buildGroups(cells);
populateReferences(cells, rows, columns, groups);
var changedAnything = false;
function applyRestrictions(cell) {
changedAnything = (cell.applyRestrictions() || changedAnything);
}
do {
changedAnything = false;
cells.forEach(applyRestrictions);
} while (changedAnything);
return cells;
};