-
Notifications
You must be signed in to change notification settings - Fork 0
/
Puzzle.cpp
466 lines (423 loc) · 12.6 KB
/
Puzzle.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// /////////////////////////////////////////////////////////
//
// File: clerdle/Puzzle.cpp
// Author: Michael Foster
// Date: 2022.04.29
//
// This file generates and tracks a new puzzle.
//
// /////////////////////////////////////////////////////////
#include "Puzzle.h"
//---------------//
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include "UX.h"
#include "Guess.h"
/* HARDGEN attempts
* the number of attempts to brute force "hard-gen" (3-term)
* problems before falling back to an algorithmic solution.
* Over testing of around 50,000 "hard-gen" generations, the favorite
* value was HGA=13, resulting in instantenous performance over 5000
* generations, and a 99% success rate.
*
* see http://cpp-moon.s.h-n.me/HGA.png for the data.
*/
#define HARDGEN_ATTEMPTS 13
// percent chance of certain puzzle formats. the 1112 format gets the remainder /100.
#define CHANCE_222 35
#define CHANCE_123 55 // this format contains many sub-formats, so should occur more often
void Puzzle::seed()
{
static bool done = false;
if (!done)
{
std::srand(std::time(0));
done = true;
}
}
// CONSTRUCTOR Puzzle() //
Puzzle::Puzzle()
{
Puzzle::seed();
// create the puzzle and add it to the object's `answer_` member
this->generateProblem();
this->setMatches();
}
Puzzle::Puzzle(std::string answer)
{
Puzzle::seed();
this->answer_ = answer;
this->setMatches();
}
/* generateProblem()
* this is the parent function for puzzle generation. note that
* throughout this class puzzle formats are referred to with a series
* of numbers, denoting the digit length for each term in the equation.
*
* e.g. format 222 ==> aa+bb=zz (but not necessarily "+")
*/
void Puzzle::generateProblem()
{
int form = rand() % 100;
if (form < CHANCE_222)
{ // aa?bb=zz [+-]
this->answer_ = eqnGenerate222();
}
else if (form >= CHANCE_222 && form < CHANCE_222 + CHANCE_123)
{ // a?bb=zzz [+*] (a or b first) -OR- zzz?a=bb [/-]
this->answer_ = eqnGenerate123();
}
else
{ // a?b?c=zz [+-*/]
std::string eqn{eqnGenerate1112()};
if (eqn.length())
this->answer_ = eqn;
else
{ // these solutions are brute forced. If too many attempts failed, switch to a different method.
this->answer_ = (rand() % 2) ? eqnGenerate123() : eqnGenerate222();
}
}
}
std::string Puzzle::eqnGenerate222()
{
int la{2}, lb{2}, lz{2}; // digit (l)ength of each argument
int a{}, b{}, z{}; // argument values
// randomly choose an operator, aka (s)ign
char s{};
if (rand() % 2)
{
s = '+';
// read about these custom math utilities at the very bottom of this file
z = randrange(min(la) + min(lb), max(lz));
a = randrange(min(la), z - min(lb));
b = z - a;
}
else
{
s = '-';
z = randrange(min(lz), max(la) - min(lb));
a = randrange(z + min(lb), max(la));
b = a - z;
}
return this->answerToString(a, s, b, z);
}
std::string Puzzle::eqnGenerate123()
{
int la{1}, lb{2}, lz{3}; // digit (l)ength of each argument
int a{}, b{}, z{}; // argument values
// randomly choose an operator, aka (s)ign
char s{(rand() % 2) ? '+' : '*'};
// read about these custom math utilities at the very bottom of this file
a = randrange(op(inv(s), min(lz), max(lb)), max(la));
b = randrange(op(inv(s), min(lz), a), max(lb));
z = op(s, a, b);
if (rand() % 2)
{ // forward case: use given operator, either + or *
// this case is commutative, so occasionally flip a and b
bool flip{bool(rand() % 2)};
return this->answerToString(flip ? a : b,
s,
flip ? b : a,
z);
}
else
{ // reverse case: use inversed operator and reorder elements
return this->answerToString(z, inv(s), a, b);
}
}
// this generator uses brute force, and *can fail*. always check for blank response.
std::string Puzzle::eqnGenerate1112()
{
std::vector<std::string> ops{"+", "-", "*", "/"};
std::vector<std::string> equation{};
std::string answer{};
int attempts = 0;
while (answer.length() != 2 && attempts < HARDGEN_ATTEMPTS)
{ // generate random equations until the desired answer length is found
equation.clear();
for (int i = 0; i < 3; i++)
{
equation.push_back(std::to_string(randrange(min(1), max(1))));
equation.push_back(ops.at(rand() % ops.size()));
}
equation.pop_back();
auto iteqn = equation; // opIterate() is destructive to the passed vector
answer = std::to_string(opIterate(iteqn));
if (answer[0] == '-')
answer = ""; // manually reject negative answers
attempts++;
}
if (attempts >= HARDGEN_ATTEMPTS) // stop trying attempts after a given number of tries
return "";
std::string out{};
for (auto s : equation)
out += s;
out += "=" + answer;
return out;
}
/* verify()
* verifies that a passed equation actually computes as written.
* the passed string is split into a vector of terms and operators,
* then the opIterate() function solves the left side of the equation.
*/
bool Puzzle::verify(std::string guess)
{
std::vector<std::string> elements;
// splitEqn splits the string into a vector of number and operator strings
int givenAnswer{splitEqn(elements, guess)};
if (!elements.size())
return false;
// run the recursive iteration function to find answer to provided calculation
int computedAnswer{opIterate(elements)};
return computedAnswer == givenAnswer;
}
// split equation string into individual elements and return the given answer
int Puzzle::splitEqn(std::vector<std::string> &elements, std::string guess)
{
std::string nbuf{};
for (char c : guess)
{
int tryint{c - '0'};
if (tryint >= 0 && tryint <= 9)
{ // if char is a digit, add to the number buffer
nbuf += c;
}
else
{ // otherwise reset the number buffer and add this char (an operator) separately
elements.push_back(nbuf);
nbuf = "";
elements.push_back(std::string(1, c));
}
}
if (!elements.size() || *(elements.end() - 1) != "=")
{ // if second to last term is not "=", fail and exit
elements.clear();
return 0;
}
// separate given answer and remove it from elements vector
elements.pop_back();
int givenAnswer{};
try
{
givenAnswer = std::stoi(nbuf);
}
catch (std::invalid_argument const &)
{ // if answer is not a number, fail and exit
elements.clear();
return 0;
}
return givenAnswer;
}
/* opIterate()
* this function iterates over an equation in string vector form. the
* vector contains numbers and operators in order. the function will
* return the solution as an integer. THIS FUNC. IS DESTRUCTIVE TO THE
* ELEMENTS VECTOR.
*/
int Puzzle::opIterate(std::vector<std::string> &elements)
{
// calculate all * and / terms
Puzzle::reduceEqnVector(elements, 0);
if (!elements.size())
return 0x7fffffff; // 01111111111111111111111111111111. max int value will always fail validation
// calculate all + and - terms
Puzzle::reduceEqnVector(elements, 1);
if (int(elements.size()) != 1) // if this is more than 1,
return 0x7fffffff;
// check that the answer is a number, then return it
int out{};
try
{
out = std::stoi(elements.at(0));
}
catch (std::invalid_argument const &)
{
return 0x7fffffff; // max int value will always fail validation
}
return out;
}
void Puzzle::reduceEqnVector(std::vector<std::string> &elements, int stage)
{
for (int i = 0; i < int(elements.size()); i++)
{
std::string el = elements.at(i);
if ((!stage && (el == "*" || el == "/")) || (stage && (el == "+" || el == "-")))
{ // look for the next operator in PEMDAS order
int a{}, b{};
try
{ // parse numbers around operator as integers
a = std::stoi(elements.at(i - 1));
b = std::stoi(elements.at(i + 1));
}
catch (std::invalid_argument const &)
{ // if there were 2 operators in a row, immediately exit
elements.clear();
break;
}
if (el[0] == '/' && (b == 0 || a % b))
{ // if any division doesn't result in an integer, immediately exit
elements.clear();
break;
}
// evaluate this operator and replace it with its solution
elements.at(i - 1) = std::to_string(op(el[0], a, b));
for (int j = 0; j < 2; j++)
elements.erase(elements.begin() + i);
// if this was successful we need to start from the beginning
i = 0;
}
}
}
void Puzzle::setMatches()
{ // find the number of each char in the correct answer for nearby char counting.
this->charMatches_.clear();
for (char c : this->answer_)
{
if (this->charMatches_[c])
this->charMatches_[c]++;
else
this->charMatches_[c] = 1;
}
}
/* compare()
* this function compares a guess string to the puzzle answer. it does
* this in 2 passes, first marking all correct and incorrect answers
* and tallying the matches for each character. then it goes back
* through to enforce the correct number of nearby matches for each
* char.
*/
Guess Puzzle::compare(std::string input) const
{
Guess guess(input);
std::map<char, int> correctCharCounts{this->charMatches_};
std::map<char, int> charMatches{};
for (auto c : this->answer_)
charMatches[c] = 0;
for (int i = 0; i < int(this->answer_.length()); i++)
{
if (i >= int(input.length())) // protect against length mismatch
break;
Guess::charState state{};
if (input[i] == this->answer_[i])
{
state = Guess::correct;
charMatches[input[i]]++;
}
else
{
bool isNearby = charMatches.find(input[i]) != charMatches.end();
if (isNearby)
state = Guess::nearby;
else
state = Guess::incorrect;
}
guess.setInd(i, {0, state});
}
std::vector<Guess::GuessChar> correctedGuess = guess.getVector();
for (Guess::GuessChar &c : correctedGuess)
{
if (c.state == Guess::nearby)
{
if (charMatches[c.character] >= correctCharCounts[c.character])
{
c.state = Guess::incorrect;
}
else
{
charMatches[c.character]++;
}
}
}
guess.set(correctedGuess);
return guess;
}
/* answerToString()
* this function concats all elements except for the last, converting
* to strings along the way. The last element passed will be separated
* by an equals sign, allowing for a generic number of operators and
* arguments in the equation.
*/
template <typename T, typename... Next>
std::string Puzzle::answerToString(T element, Next... next)
{ // if there are more arguments left, add the current arg to stringstream and call again
this->answerstream_ << element;
return this->answerToString(next...);
}
template <typename T>
std::string Puzzle::answerToString(T answer)
{ // if this is the last element, concat this element after an equals sign, then output to a string
this->answerstream_ << "=" << answer;
std::string out{this->answerstream_.str()};
this->answerstream_.str("");
return out;
}
/* \/ MATH UTILITIES BEGIN HERE \/ */
int Puzzle::op(char op, int a, int b)
{ // do math operation based on passed operator
int out{};
switch (op)
{
case '+':
out = a + b;
break;
case '-':
out = a - b;
break;
case '*':
out = a * b;
break;
case '/':
out = int(std::ceil(double(a) / double(b)));
break;
}
return out;
}
char Puzzle::inv(char op)
{ // returns the inverse operator of the given operator
char out{};
switch (op)
{
case '+':
out = '-';
break;
case '-':
out = '+';
break;
case '*':
out = '/';
break;
case '/':
out = '*';
break;
}
return out;
}
int Puzzle::min(int digits)
{ // finds the minimum value for an x-digit number
// the decision was made to exclude the number 0, as many of the
// associated puzzles feel "cheap", and not as fun.
if (!digits)
return 0;
std::string num{"1"};
for (int i = 1; i < digits; i++)
{
num += "0";
}
return std::stoi(num);
}
int Puzzle::max(int digits)
{ // finds the maximum value for an x-digit number
if (!digits)
return 0;
std::string num{};
for (int i = 0; i < digits; i++)
{
num += "9";
}
return std::stoi(num);
}