-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrids.cpp
More file actions
100 lines (94 loc) · 2.33 KB
/
Copy pathgrids.cpp
File metadata and controls
100 lines (94 loc) · 2.33 KB
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
#include "grids.h"
#include "solver.h"
#include "rowminlex.h"
void gridsWithPuzzles::addPuzzle(const char *p) {
ch81 tp, tg;
char pp[2][81];
char buf[100];
if(solve(p, 2, pp[0]) != 1) {
ch81 pbuf;
pbuf = *(ch81*)p;
pbuf.toString(buf);
printf("Multisolution puzzle: %81.81s\n", buf);
return; //wrong puzzle
}
transformer tr;
tr.byGrid(pp[0]);
//if(tr.aut > 1) {
// ch81 pbuf;
// pbuf = *(ch81*)pp[0];
// pbuf.toString(buf);
// printf("Automorphic grid: (%d) %81.81s\n", tr.aut, buf);
//}
tr.transform(pp[0], tg.chars); //the grid
tr.transform(p, tp.chars); //the puzzle
puzzlesInGrid &pg = (*this)[tg];
if(pg.count(tp)) {
tp.toString(buf);
printf("Duplicate puzzle: %81.81s\n", buf);
}
pg.insert(tp);
}
void gridsWithPuzzles::loadFromFile(const char *fname) {
FILE *file;
file = fopen(fname, "rt");
if (! file)
return;
loadFromFile(file);
fclose(file);
}
void gridsWithPuzzles::loadFromFile(FILE *file) {
char buf[1000];
while(fgets(buf, sizeof(buf), file)) {
ch81 puz;
puz.fromString(buf);
addPuzzle(puz.chars);
}
}
void gridsWithPuzzles::saveToFile(FILE * file, const bool noPuz, const bool verbose) {
char buf[82];
ch81 gIntersection, gUnion;
buf[81] = 0;
for(const_iterator g = begin(); g != end(); g++) {
g->first.toString(buf);
fprintf(file, "%81.81s", buf);
if(!noPuz) {
fprintf(file, "\t%d", (int)(g->second.size()));
if(verbose) {
for(int i = 0; i < 81; i++) {
gIntersection.chars[i] = g->first.chars[i];
gUnion.chars[i] = 0;
}
}
for(puzzlesInGrid::const_iterator p = g->second.begin(); p != g->second.end(); p++) {
p->toString(buf);
fprintf(file, "\t%81.81s", buf);
if(verbose) {
for(int i = 0; i < 81; i++) {
gIntersection.chars[i] &= p->chars[i];
gUnion.chars[i] |= p->chars[i];
}
}
}
}
fprintf(file, "\n");
if(verbose) {
gIntersection.toString(buf);
fprintf(file, "\t%81.81s\tintersection\n", buf);
gUnion.toString(buf);
fprintf(file, "\t%81.81s\tunion\n", buf);
}
}
}
void gridsWithPuzzles::reindexBySize() {
indexBySize.clear();
for(const_iterator g = begin(); g != end(); g++) {
indexBySize[(int)g->second.size()].insert((void*)&(g->first));
}
}
extern int groupBySolution(const bool noPuz, const bool verbose) {
gridsWithPuzzles gp;
gp.loadFromFile(stdin);
gp.saveToFile(stdout, noPuz, verbose);
return 0;
}