-
Notifications
You must be signed in to change notification settings - Fork 0
/
dobble_solver.cpp
274 lines (233 loc) · 6.7 KB
/
dobble_solver.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
#include <array>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <chrono>
#define template_header int N, int U=N*(N-1)+1
struct Logo {
int id;
Logo() : id(-1) { }
Logo(int id) : id(id) { }
void next() {
++id;
}
bool operator==(const Logo& other) const { return id == other.id; }
bool operator!=(const Logo& other) const { return id != other.id; }
};
template<template_header>
struct Card {
Card() : logos(), active(), nz(0) {
std::fill(active.begin(), active.end(), 0);
}
std::array<Logo, N> logos;
std::array<int, U> active;
int nz;
bool compatibleWith(const Card& other) const {
check();
other.check();
int collisions = 0;
if(nz == 0) return true;
if(other.active[logos[nz-1].id] == 0) return true;
for(int i = 0; i < other.nz; ++i) {
int id = other.logos[i].id;
if(active[id]) collisions++;
}
return collisions <= 1;
}
bool push() {
assert(nz < N);
int id = (nz == 0 ? 0 : logos[nz-1].id+1);
if(id >= U) return false;
logos[nz] = Logo(id);
if(id < U) active[id] = 1;
++nz;
return true;
}
void push(Logo l) {
assert(nz < N);
logos[nz] = l;
assert(l.id >= 0 && l.id < U);
active[l.id] = 1;
++nz;
}
bool valid() const {
return (nz == N) && std::all_of(logos.begin(), logos.begin()+nz, [](const Logo& l){ return l.id < U; });
}
bool next() {
assert(logos[nz-1].id >= 0 && logos[nz-1].id < U);
active[logos[nz-1].id] = 0;
logos[nz-1].next();
bool ok = (logos[nz-1].id < U);
if(ok) {
active[logos[nz-1].id] = 1;
}
return ok;
}
void check() const {
for(int i = 0; i < nz; ++i) {
assert(logos[i].id >= 0 && logos[i].id < U);
assert(active[logos[i].id]);
}
int acc = 0;
for(int ac : active) acc += ac;
assert(acc == nz);
}
std::string toString() const {
std::string s;
for(int i = 0; i< nz; ++i) s += ofLogo(logos[i]) + " ";
return s;
}
std::string ofLogo(Logo l) const {
std::string s;
int id = l.id;
if(id < N) {
s += std::to_string(id) + " ";
} else {
id = id-N;
s += std::to_string(id%(N-1)) + char(65+(id)/(N-1));
}
return s;
}
};
template<template_header>
struct Solution {
std::vector<Card<N,U>> cards;
bool nil;
Solution() : cards(), nil(false) {}
bool violates() const {
if(nil) return true;
if(cards.size() > U) return true;
if(cards.back().logos.size() < N && cards.back().logos.back() == U) return true;
for(int i = 0; i < cards.size()-1; ++i) {
if(!cards.back().compatibleWith(cards[i])) return true;
}
return false;
}
bool valid() const {
if(nil) return false;
if(cards.size() != U) return false;
if(!std::all_of(cards.begin(), cards.end(), [](const Card<N,U>& c){ return c.valid(); })) return false;
for(int i = 0; i < cards.size(); ++i) {
for(int j = i+1; j < cards.size(); ++j) {
if(!cards[i].compatibleWith(cards[j])) return false;
}
}
return true;
}
void push() {
if(cards.empty()) {
cards.emplace_back();
} else {
if(cards.back().nz == N) {
cards.emplace_back();
}
}
if(!cards.back().push()) {
nil = true;
}
}
void next() {
if(!cards.back().next()) {
nil = true;
}
}
bool isNil() const {
return nil;
}
std::string toString() const {
std::string s;
s += "nil?" + std::to_string(nil) + " ";
s += "s" + std::to_string(cards.size()) + '\n';
for(const Card<N,U>& c : cards) s += c.toString() + '\n';
return s;
}
};
template<template_header>
struct Solver {
Solution<N, U> root() {
Solution<N, U> r;
for(int i = 0; i < 1+2*(N-1); ++i) { r.cards.emplace_back(); }
// first card
for(int i = 0; i < N; ++i) {
r.cards[0].push(Logo{i});
}
// first "column" : cards with 0
for(int c = 0; c < N-1; ++c) {
r.cards[1+c].push(Logo{0});
}
int l = N;
for(int c = 0; c < N-1; ++c) {
for(int i = 1; i < N; ++i) {
r.cards[1+c].push(Logo{l});
++l;
}
}
// first "column" : cards with 1
for(int c = 0; c < N-1; ++c) {
r.cards[N+c].push(Logo{1});
}
l = N;
for(int i = 1; i < N; ++i) {
for(int c = 0; c < N-1; ++c) {
r.cards[N+c].push(Logo{l});
++l;
}
}
std::cout << "Root : " << r.toString() << "\n";
return r;
}
bool reject(const Solution<N, U>& sol) {
return sol.violates();
}
bool accept(const Solution<N, U>& sol) {
return sol.valid();
}
Solution<N,U> first(const Solution<N, U>& candidate) {
Solution<N, U> s = candidate;
s.push();
return s;
}
void next(Solution<N, U>& sol) {
sol.next();
}
long long calls = 0;
std::chrono::system_clock::time_point begin;
std::chrono::system_clock::time_point current;
void backtrack(Solution<N, U>& candidate) {
calls++;
if(calls % 1000000 == 0) {
current = std::chrono::high_resolution_clock::now();
auto elapsed = current - begin;
std::cout << "\x1B[2J\x1B[H";
std::cout
<< (calls / 1.0e6) << " Mcalls\n"
<< (calls / 1.0e6) / (elapsed.count() / 1.0e9) << " Mcalls/s" << '\n'
//<< elapsed.count() << " t"
<< candidate.toString()
<< std::endl;
}
if(reject(candidate)) return;
if(accept(candidate)) {
std::cout << "Solution found" << std::endl;
std::cout << candidate.toString() << std::endl;
std::cout << "Total calls : " << calls << "\n";
std::exit(0);
}
Solution<N, U> s = first(candidate);
while(!s.isNil()) {
backtrack(s);
next(s);
}
}
Solver() {
begin = std::chrono::high_resolution_clock::now();
}
};
int main(int argc, const char* argv[]) {
constexpr int N = 6;
Solver<N> s;
Solution<N> sol = s.root();
s.backtrack(sol);
std::cout << "Total calls : " << s.calls << "\n";
}