-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudosolve.cpp
298 lines (255 loc) · 7.38 KB
/
sudosolve.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include "coords.h"
#include "psout.h"
#include "svgout.h"
#include "backtrack_common.h"
#include "backtrack_naive_unique.h"
#include "bitmatrix_to_ps.h"
#include "bits.h"
#include "simple_naked_single.h"
#include "naked_single.h"
#include "hidden_single.h"
#include "nh_subset.h"
#include "pointing.h"
#include "boxline.h"
#include "nfish.h"
#include "simple_coloring.h"
#include "ywing.h"
#include "pretty_solver.h"
#include "pattern.h"
#include "backtrack_to_bits.h"
#include "permutation.h"
#include "bits_to_output.h"
#include "strong_link.h"
#include "simple_complete_gen.h"
#include "naive_gen.h"
// constants
const int OUT_PS = 0;
const int OUT_SVG = 1;
// initial variables
int output_format = 0;
bool show_patterns = false;
bool show_candidates = false;
bool show_keys = false;
std::string font = "Helvetica";
std::string prefix = "sudoku";
Pattern* find_pattern(Bits& bits) {
// finds the first possible pattern, and returns a copy of it to process
std::vector<SimpleNakedSingle> snss = find_simple_naked_singles(&bits);
if(snss.size() > 0) {
return snss[0].clone();
}
std::vector<NakedSingle> nss = find_naked_singles(&bits);
if(nss.size() > 0) {
return nss[0].clone();
}
std::vector<HiddenSingle> hss = find_hidden_singles(&bits);
if(hss.size() > 0) {
return hss[0].clone();
}
std::vector<NHSubset> nhs = find_nh_subsets(&bits);
if(nhs.size() > 0) {
return nhs[0].clone();
}
std::vector<Pointing> points = find_pointings(&bits);
if(points.size() > 0) {
return points[0].clone();
}
std::vector<BoxLine> boxlines = find_boxlines(&bits);
if(boxlines.size() > 0) {
return boxlines[0].clone();
}
std::vector<NFish> nfishes = find_nfishes(&bits);
if(nfishes.size() > 0) {
return nfishes[0].clone();
}
std::vector<SimpleColoring> scs = find_simple_colorings(&bits);
if(scs.size() > 0) {
return scs[0].clone();
}
std::vector<YWing> ywings = find_ywings(&bits);
if(ywings.size() > 0) {
return ywings[0].clone();
}
return NULL;
}
void do_help() {
// help message
std::cerr << "Usage: sudosolve [options]\n";
std::cerr << "Outputs the logical solving process for a Sudoku puzzle in postscript or SVG.\n";
std::cerr << "\n";
std::cerr << "Options:\n";
std::cerr << " -p, --postscript Output six grids a page in postscript format\n";
std::cerr << " Saved in <PREFIX>.ps\n";
std::cerr << " -s, --svg Output in multiple SVG files of the form:\n";
std::cerr << " <PREFIX>000.svg, <PREFIX>001.svg, ...\n";
std::cerr << " -c, --candidates Display possible candidates in each cell\n";
std::cerr << " -t, --patterns Display a visual indication of the pattern used\n";
std::cerr << " to solve each step. Also turns on candidates.\n";
std::cerr << " -k, --keys Outline cells that the current pattern depends upon\n";
std::cerr << " -h, --help Will display this message\n";
std::cerr << " -f, --font=FONT Will use FONT for postscript output\n";
std::cerr << " Default: Helvetica\n";
std::cerr << " -o, --prefix=PREFIX Output file(s) will be given this prefix.\n";
std::cerr << " Default: 'sudoku'\n";
std::cerr << "\n";
std::cerr << "Input will be taken from standard input in the dotted form, for example:\n";
std::cerr << ".4398.25.6..425...2....1.949....4.7.3..6.8...41.2.9..382.5.........4...553489.71.\n";
std::cerr << "\n";
std::cerr << "is equivalent to the grid:\n";
std::cerr << ".4398.25.\n";
std::cerr << "6..425...\n";
std::cerr << "2....1.94\n";
std::cerr << "9....4.7.\n";
std::cerr << "3..6.8...\n";
std::cerr << "41.2.9..3\n";
std::cerr << "82.5.....\n";
std::cerr << "....4...5\n";
std::cerr << "53489.71.\n";
std::cerr << "\n";
std::cerr << "where in both cases, .'s represent blank cells in the Sudoku grid.\n";
exit(1);
}
int main(int argc, char* argv[]) {
// parse command line arguments
int c;
while(true) {
static struct option long_options[] = {
{"postscript", no_argument, 0, 'p'},
{"svg", no_argument, 0, 's'},
{"patterns", no_argument, 0, 't'},
{"candidates", no_argument, 0, 'c'},
{"keys", no_argument, 0, 'k'},
{"help", no_argument, 0, 'h'},
{"font", required_argument, 0, 'f'},
{"prefix", required_argument, 0, 'o'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "pstckf:o:", long_options, &option_index);
if(c == -1) { break; }
switch(c) {
/*
case 0:
*/
case 'p':
output_format = OUT_PS; break;
case 's':
output_format = OUT_SVG; break;
case 't':
show_patterns = true; show_candidates = true; break;
case 'c':
show_candidates = true; break;
case 'k':
show_keys = true; break;
case 'h':
do_help(); break;
case 'f':
font = std::string(optarg); break;
case 'o':
prefix = std::string(optarg); break;
}
}
if(output_format == OUT_SVG && font != "Helvetica") {
std::cerr << "WARNING: SVG output currently supports only Helvetica fonts." << std::endl;
std::cerr << "WARNING: Overriding and using Helvetica anyways." << std::endl;
}
// load puzzle from standard input
Bits bits = Bits();
std::cin >> bits;
// initialize variables needed for output
bool running = true;
Psout ps = Psout();
ps.init();
SVGOut svg = SVGOut();
svg.init();
if(output_format == OUT_PS) {
prefix.append(".ps");
ps.start_file(prefix.c_str());
}
int pageidx = 0;
int frameidx = 0;
std::ofstream out;
while(running) {
Pattern* pat = find_pattern(bits);
if(pat == NULL) {
running = false;
}
if(output_format == OUT_PS) {
ps.init();
write_numbers(&bits, &ps);
if(show_candidates) {
write_candidates(&bits, &ps);
}
if(show_patterns && running) {
pat->full_display(&ps);
}
if(show_keys && running) {
pat->display_keys(&ps);
}
out.open(prefix.c_str(), std::ios::app | std::ios::out);
// override font from header.
out << "/fullfont {/" << font << " findfont 20 scalefont setfont} def\n/candidatefont {/" << font << " findfont 8 scalefont setfont} def\n";
out << "gsave\n";
if(pageidx % 2 == 0) {
out << "58 ";
} else {
out << "311 ";
}
if(pageidx / 2 == 0) {
out << "527 translate\n";
} else if(pageidx / 2 == 1) {
out << "274 translate\n";
} else {
out << "21 translate\n";
}
out.close();
ps.append_file_barebones(prefix.c_str());
out.open(prefix.c_str(), std::ios::app | std::ios::out);
out << "makegrid\n";
out << "grestore\n";
pageidx++;
if(pageidx == 6) {
pageidx = 0;
out << "showpage\n";
}
out.close();
} else if(output_format == OUT_SVG) {
svg.init();
write_numbers(&bits, &svg);
if(show_candidates) {
write_candidates(&bits, &svg);
}
if(show_patterns && running) {
pat->full_display(&svg);
}
if(show_keys && running) {
pat->display_keys(&svg);
}
char buf[10];
sprintf(buf, "%03d.svg",frameidx);
frameidx++;
std::string filename = prefix;
filename.append(buf);
out.open(filename.c_str(), std::ios::out);
svg.draw(out);
out.close();
}
if(running) {
pat->apply(&bits);
delete pat;
}
}
if(output_format == OUT_PS) {
if(pageidx != 0) {
out.open(prefix.c_str(), std::ios::app | std::ios::out);
out << "showpage\n";
out.close();
}
}
return 0;
}