-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.hpp
262 lines (207 loc) · 5.44 KB
/
solver.hpp
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
#pragma once
#include <cassert>
#include <cstdint>
#include <functional>
#include <sstream>
#include <vector>
#include "callbackQueue.hpp"
#include "state.hpp"
#include "staticData.hpp"
class Solver
{
private:
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u64 = std::uint64_t;
State mState;
std::vector<State>* mCurrSolutions = nullptr;
CallbackQueue mDirtyCells;
bool mContradiction = false;
void ProcessCell(u8 cellNo)
{
u16 cell = mState.GetCell(cellNo);
for (u8 neighbourNo : GetCellNeighbours(cellNo))
{
u16 neighbour = mState.GetCell(neighbourNo);
if ((neighbour & cell) != 0u)
{
neighbour &= ~cell;
if ((neighbour & (neighbour - u16(1))) == 0u) // TODO: try getting rid of all instances of u8(1) etc.
{
if (neighbour == 0u)
{
mContradiction = true;
return;
}
mDirtyCells.Push(neighbourNo);
}
}
mState.SetCell(neighbourNo, neighbour);
}
}
static bool CellIsPair(u16 cell) // cell != 0 is assumed
{
cell &= (cell - u16(1));
if (cell == 0)
return false;
cell &= (cell - u16(1));
return (cell == 0);
}
void LockedPairsInGroup(const std::array<u8, 9>& group)
{
u16 lockedPair = 511;
for (u8 i = 0u; i != 9u; ++i)
{
u16 cell = mState.GetCell(group[i]);
if (CellIsPair(cell))
{
lockedPair = cell;
break;
}
}
if (lockedPair == 511)
return;
u8 count = 0u;
for (u8 i = 0u; i != 9u; ++i)
{
u16 cell = mState.GetCell(group[i]);
count += (cell == lockedPair);
}
if (count != 2)
{
mContradiction |= (count > 2);
return;
}
for (auto cellNo : group)
{
u16 cell = mState.GetCell(cellNo);
if ((cell & lockedPair) != 0u)
{
if (cell == lockedPair)
continue; // TODO: duplicating counter here?
cell &= ~lockedPair;
if ((cell & (cell - u16(1))) == 0u)
{
if (cell == 0u)
{
mContradiction = true;
return;
}
mDirtyCells.Push(cellNo); // TODO: try going back to normal strategy at first occurrence here
}
}
mState.SetCell(cellNo, cell);
}
}
void LockedPairs()
{
for (auto& group : GetGroups())
{
LockedPairsInGroup(group);
if (!mDirtyCells.Empty())
return;
}
}
std::string GetDirtyCellsInBinary() const
{
std::string result(81, '0');
auto dirtyCells = mDirtyCells;
while (true)
{
u8 cellNo = dirtyCells.Pop();
if (cellNo == u8(255))
break;
result[cellNo] = '1';
}
return result;
}
void ProcessCurrState()
{
while (!mDirtyCells.Empty())
{
ProcessCurrStateRaw();
if (mContradiction)
return;
LockedPairs();
if (mContradiction)
return;
}
}
void ProcessCurrStateRaw()
{
while (!mDirtyCells.Empty())
{
u8 cellNo = mDirtyCells.Pop();
ProcessCell(cellNo);
if (mContradiction)
return;
}
}
void MarkFinalCellsDirty()
{
for (u8 cellNo = 0u; cellNo != 81u; ++cellNo)
{
u16 cell = mState.GetCell(cellNo);
if ((cell & (cell - u16(1))) == 0u)
{
mDirtyCells.Push(cellNo);
}
}
}
void Guess(u8 cellNo, u16 cell)
{
mState.SetCell(cellNo, cell);
mDirtyCells.Push(cellNo);
}
void SolveImpl()
{
ProcessCurrState();
ProcessingExhausted();
}
void ProcessingExhausted()
{
if (mContradiction)
{
mContradiction = false;
return;
}
u8 guessCellNo = mState.PickNonFinalisedCell();
if (guessCellNo == u8(255))
{
mCurrSolutions->push_back(mState);
return;
}
u16 cell = mState.GetCell(guessCellNo);
u16 newCell = u16(1) << u16(8);
State backupState = mState;
while (newCell > u16(0))
{
if ((newCell & cell) != u16(0))
{
Guess(guessCellNo, newCell);
SolveImpl();
mState = backupState;
mDirtyCells.Clear();
}
newCell >>= u16(1);
}
}
public:
State ShallowSolve(const State& state)
{
mState = state;
MarkFinalCellsDirty();
ProcessCurrState();
return mState;
}
std::vector<State> Solve(const State& state)
{
std::vector<State> solutions;
mCurrSolutions = &solutions;
mState = state;
MarkFinalCellsDirty();
SolveImpl();
mCurrSolutions = nullptr;
return solutions;
}
};