forked from naraen/sudokusolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (181 loc) · 6.2 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function () {
"use strict";
const repl = require("./repl.js");
const Grid = require("./grid.js").grid;
const gridSetLogLevel = require("./grid.js").setLogLevel;
const readline = require("readline");
//TODO: propagate naked twins.
//TODO: Rename findsingle to findOnlyOnce
//TODO: Rename sets to peers
//TODO: query for solved count
//TODO: solve count at the end of every operation.
//TODO: rewind hint
//TODO: investigate why parser error spew shown in stdout/stderr is not suppresed by try-catch - Don
//TODO: fix ambiguity in grammar. Done.
//TODO: add help command in grammar
//TODO: forgive lack of spacing in cellIdx = value syntax. Done
//TODO: implement quit command in the grammar - DONE
//TODO: Enforce numbers to be int
//TODO: Change internal cellIdx to be RowCol reference instead of array index reference
var inputThroughConsole = "";
var boolIsDoneReceiving = true;
var gridFromConsoleInput;
var hintHistory = [];
var stash = [];
var color=32;
function receiveInput(input) {
inputThroughConsole += (input || "").toString().replace(/[^0-9]/g, "");
boolIsDoneReceiving =
inputThroughConsole.replace(/[ \n\t]/g, "").length == 81;
if (boolIsDoneReceiving) {
var formattedInput = inputThroughConsole;
console.log(
"Received Input",
"\n\t" + inputThroughConsole.replace(/([0-9]{9})/g, "$1\n\t")
);
gridFromConsoleInput = new Grid(inputThroughConsole);
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
rl.on("line", (line) => {
if (line.indexOf("quit") > -1) {
rl.close();
return;
}
if (!boolIsDoneReceiving) {
receiveInput(line);
return;
}
try {
var { commandId, command } = repl.parseInput(line);
if (commandId != undefined) {
runCommand(commandId, command);
}
} catch (e) {
console.error("Error while parsing input", line);
console.error(e);
console.log("¯\\_(ツ)_/¯!");
}
});
function inputToCellIdx(inp) {
return parseInt(inp / 10 - 1) * 9 + (inp % 10) - 1;
}
function runCommand(commandId, command) {
switch (commandId) {
case "init_grid":
inputThroughConsole = "";
hintHistory = [];
receiveInput(command.numbers);
break;
case "show_grid":
var formattedGrid = gridFromConsoleInput.getGridForDisplay();
if (command.numbers != undefined) {
//TODO: why is numbers not being post processed?
//https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
//Highlighing number in red.
var pattern = command.numbers.value;
formattedGrid = formattedGrid.replaceAll(
pattern,
"\x1b[1m\x1b[" + color + "m" + pattern + "\x1b[0m"
);
}
console.log(formattedGrid);
break;
case "rewind_grid":
if (stash.length > 0) {
var prevState = stash.pop();
gridFromConsoleInput = new Grid(prevState);
}
break;
case "reset_grid":
if (inputThroughConsole.length != 81) {
console.log("! No current grid");
return;
}
gridFromConsoleInput = new Grid(inputThroughConsole);
break;
case "show_input":
console.log(`\n\t${inputThroughConsole
.replace(/([0-9]{3})/g, "$1 ")
.replace(/([0-9 ]{12})/g, "$1\n\t")}
`);
break;
case "show_unsolved_count":
console.log(gridFromConsoleInput.unsolvedCount(), "unsolved cells");
break;
case "show_hint_history":
console.log(JSON.stringify(hintHistory, null, 2));
break;
case "is_it_solved":
console.log(gridFromConsoleInput.isSolved() ? "Yes" : "No");
break;
case "is_it_stuck":
console.log(gridFromConsoleInput.isHalted() ? "Yes" : "No");
break;
case "is_it_correct":
console.log(gridFromConsoleInput.checkForCorrectness() ? "Yes" : "No");
break;
case "use_only_choice":
var currentState = gridFromConsoleInput.serialize();
stash.push(currentState);
gridFromConsoleInput.findSingleCandidates();
break;
case "use_brute_force":
var currentState = gridFromConsoleInput.serialize();
stash.push(currentState);
var hints = gridFromConsoleInput.useBruteForce();
console.log(`hints = ${JSON.stringify(hints)}`);
break;
case "set_color":
console.log(command);
color=parseInt(command.numbers.value);
break;
case "set_debug":
gridSetLogLevel(command.qualifier == 'on' ? "Debug" : 'NOP');
break;
case "use_hint":
var { cellIdx, value } = command;
if (isNaN(cellIdx) || isNaN(value)) {
console.log("could not parse hints", command);
} else {
cellIdx = inputToCellIdx(cellIdx);
console.log(`Received hint. ${cellIdx} = ${value}`);
hintHistory.push([cellIdx, value]);
var currentState = gridFromConsoleInput.serialize();
stash.push(currentState);
gridFromConsoleInput.useHints([[cellIdx, value]]);
}
break;
case "set_value":
var { cellIdx, value } = command;
if (isNaN(cellIdx) || isNaN(value)) {
console.log("could not parse command", command);
} else {
cellIdx = inputToCellIdx(cellIdx);
console.log(`Applying. ${cellIdx} = ${value}`);
var currentState = gridFromConsoleInput.serialize();
stash.push(currentState);
gridFromConsoleInput.setValue(cellIdx, value);
}
break;
case "remove_value":
var { cellIdx, value } = command;
if (isNaN(cellIdx) || isNaN(value)) {
console.log("could not parse command", command);
} else {
cellIdx = inputToCellIdx(cellIdx);
console.log(`Applying. ${cellIdx} != ${value}`);
var currentState = gridFromConsoleInput.serialize();
stash.push(currentState);
gridFromConsoleInput.removeCandidate(cellIdx, value);
}
break;
default:
console.log("¯\\_(ツ)_/¯");
}
}
})();