-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboggle.cxx
More file actions
466 lines (404 loc) · 14.2 KB
/
boggle.cxx
File metadata and controls
466 lines (404 loc) · 14.2 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
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
/***********************************************
3D Boggle Solver
for Connected Signals hiring process
Samantha Mintzmyer
***********************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <math.h>
#include <cstdio>
#include <ctime>
#include <set>
/*************************************
Methods for dictionary:
dictLookup checks if word is in hashtable
checkPrefix checks if the hashtable contains a word with the given prefix
*************************************/
class WordWizard
{
// Data types
protected:
// Prefix Dictionary
std::set<std::string> PrefixDict;
// Word Dictionary
std::unordered_set<std::string> WordDict;
// Lookup History
std::unordered_multimap<std::string, bool> History;
// Methods
public:
// Prefix Dictionary and Checker
void buildHash(std::string);
bool dictLookup(std::string);
// Word Dictionary and Checker
void buildSet(std::string);
bool checkPrefix(std::string);
// History checker
int checkHistory(std::string);
};
// buildHash: accepts dictionary file, returns hashtable of words
void WordWizard::buildHash(std::string dictName)
{
// Read words into hashtable
std::ifstream infile(dictName);
std::string s;
while (std::getline(infile, s))
{
this->PrefixDict.insert (s);
}
}
// buildSet: accepts dictionary file, returns hashtable of words
void WordWizard::buildSet(std::string dictName)
{
// Read words into hashtable
std::ifstream infile(dictName);
std::string s;
while (std::getline(infile, s))
{
WordDict.insert (s);
}
}
// dictLookup: accepts word and dictionary, returns true/false
bool WordWizard::dictLookup(std::string word)
{
return this->WordDict.count(word) > 0;
}
// checkPrefix: accepts word and dictionary, returns if prefix exists
bool WordWizard::checkPrefix(std::string word)
{
// std::cout << "Inside checkPrefix with " << word << std::endl;
int historyResult = checkHistory(word);
// std::cout << "Completed historyResult, returned " << historyResult << std::endl;
if (historyResult != -1)
{
return historyResult;
}
else
{
std::set<std::string>::iterator prefix;
prefix = PrefixDict.lower_bound(word);
std::string result = *prefix;
bool test = false;
if (result.length() >= word.length())
{
result = result.substr(0, word.length());
test = (0 == result.compare(word));
}
std::pair<std::string, bool> prefixResult (word, test);
this->History.insert(prefixResult);
return test;
}
}
// checkHistory: accepts word, returns true/false
int WordWizard::checkHistory(std::string word)
{
//std::cout << "In checkHistory with " << word << std::endl;
if (! (this->History.count(word) > 0))
{
//std::cout << "Word not in history " << std::endl;
return -1;
}
else
{
auto findResult = this->History.find(word);
//std::cout << "findResult returned " << findResult->second << std::endl;
if (findResult->second == true)
{
return 1;
}
else if (findResult->second == false)
{
return 0;
}
}
}
/*************************************
Data Structure for Cubies
Individual cell to comprise Cube
Methods to initialize Cubie
*************************************/
class Cubie
{
public:
bool used;
int neighbors;
std::string letter;
std::multimap<std::string*,Cubie*> nextTo;
//Cubie ** nextTo;
Cubie();
void setLetter(std::string);
void addNeighbor(Cubie *);
void printConnections();
void trashCubie();
};
Cubie::Cubie()
{
this->neighbors = 0;
this->used = false;
// this->nextTo = new Cubie*[26];
}
void Cubie::setLetter(std::string inLetter)
{
letter = inLetter;
}
void Cubie::addNeighbor(Cubie * cell)
{
// std::cout << "Index: " << this->neighbors << std::endl;
// Should this really be Cubie**? Maybe Cubie*...
std::pair<std::string*, Cubie*> neighbor (&(cell->letter), cell);
this->nextTo.insert (neighbor);
}
void Cubie::printConnections()
{
for (int i = 0; i < neighbors; i++)
{
//std::cout << nextTo[i]->letter << " ";
}
std::cout << std::endl;
}
void Cubie::trashCubie()
{
//delete[] this->nextTo;
}
/*************************************
Data Structure for Cube
3D array of Cubies
Methods to initialize Cube
*************************************/
class Cube
{
public:
int size;
int totalWords = 0;
Cubie ** Cubies;
std::unordered_set<std::string> wordsFound;
Cube(int);
void setConnections();
void setLetters(std::string);
bool checkWordsFound(std::string);
void printCube();
void garbage();
void traverse(Cubie*, std::string, WordWizard*);
};
/*****************************************
Initialize Cube
4 Cubie 'types' to consider:
Central - 26 Connections
Side - 17 Connections
Edge - 11 Connections
Corner - 7 Connections
*****************************************/
Cube::Cube(int inSize)
{
this->size = inSize;
this->Cubies = new Cubie*[size*size*size];
// Create sizeXsizeXsize cubies for the cube
for (int c = 0; c < pow(size, 3); c++)
{
this->Cubies[c] = new Cubie();
// std::cout << "New: " << this->Cubies[c]->neighbors << std::endl;
}
}
void Cube::setConnections()
{
// Populate cubie nextTo array with pointer to all cubies neighboring
// Iterate through the ith layer of the cube
bool front, back, top, bottom, left, right;
for (int i = 0; i < size; i++)
{
// Iterate through the jth row of the ith layer
for (int j = 0; j < size; j++)
{
// Iterate through the kth column of the jth row of the ith layer
for (int k = 0; k < size; k++)
{
if (i > 0) front = false;
else front = true;
if (i < this->size-1) back = false;
else back = true;
if (j > 0) top = false;
else top = true;
if (j < this->size-1) bottom = false;
else bottom = true;
if (k > 0) left = false;
else left = true;
if (k < this->size-1) right = false;
else right = true;
// Six faces to cube
if (!top) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[(i*16)+((j-1)*4)+k]);
if (!bottom) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[(i*16)+((j+1)*4)+k]);
if (!front) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+(j*4)+k]);
if (!back) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+(j*4)+k]);
if (!left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[(i*16)+(j*4)+k-1]);
if (!right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[(i*16)+(j*4)+k+1]);
// Twelve edges to cube
if (!top && !left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i)*16)+((j-1)*4)+k-1]);
if (!top && !right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i)*16)+((j-1)*4)+k+1]);
if (!bottom && !left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i)*16)+((j+1)*4)+k-1]);
if (!bottom && !right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i)*16)+((j+1)*4)+k+1]);
if (!top && !front) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j-1)*4)+k]);
if (!top && !back) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j-1)*4)+k]);
if (!bottom && !front) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j+1)*4)+k]);
if (!bottom && !back) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j+1)*4)+k]);
if (!right && !front) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j)*4)+k+1]);
if (!right && !back) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j)*4)+k+1]);
if (!left && !front) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j)*4)+k-1]);
if (!left && !back) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j)*4)+k-1]);
// Eight corners to cube
if (!top && !front && !right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j-1)*4)+k+1]);
if (!top && !back && !right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j-1)*4)+k+1]);
if (!bottom && !front && !right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j+1)*4)+k+1]);
if (!bottom && !back && !right) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j+1)*4)+k+1]);
if (!top && !front && !left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j-1)*4)+k-1]);
if (!top && !back && !left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j-1)*4)+k-1]);
if (!bottom && !front && !left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i-1)*16)+((j+1)*4)+k-1]);
if (!bottom && !back && !left) Cubies[(i*16)+(j*4)+k]->addNeighbor(Cubies[((i+1)*16)+((j+1)*4)+k-1]);
}
}
}
}
void Cube::setLetters(std::string cubeLetters)
{
for (int i = 0; i < pow(size, 3); i++)
{
Cubies[i]->setLetter( std::string(1, cubeLetters[i]) );
}
}
// checkWordsFound: accepts word, returns true/false
bool Cube::checkWordsFound(std::string word)
{
return this->wordsFound.count(word) > 0;
}
void Cube::printCube()
{
std::cout << "Cube contents:\n" << std::endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
for (int k = 0; k < size; k++)
{
std::cout << Cubies[(4*i)+(16*j)+k]->letter << " ";
}
std::cout << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void Cube::garbage()
{
for (int c = 0; c < pow(size, 3); c++)
{
Cubies[c]->trashCubie();
delete this->Cubies[c];
}
delete[] this->Cubies;
}
/*************************************
Method for word search
Traverses cube comparing to hashtable
*************************************/
void Cube::traverse(Cubie * cell, std::string word, WordWizard* library)
{
// std::cout << "Inside traverse with " << word << std::endl;
// Check if word is in dictionary
if ( (!checkWordsFound(word)) && ((*library).dictLookup(word) ))
{
// std::cout << "Completed library dictLookup" << std::endl;
this->totalWords++;
this->wordsFound.insert(word);
}
cell->used = true;
// Search unique elements of nextTo multimap for valid prefix
std::string letter;
for (std::multimap<std::string*, Cubie*>::iterator Neighbor = cell->nextTo.begin(); Neighbor != cell->nextTo.end(); )
{
letter = *(Neighbor->first);
word = word + letter;
// std::cout << "Entered new neighbor iterator: " << word << std::endl;
// Traverse all unused valid prefix elements
// std::cout << "Word = " << word << " Letter = " << letter << " used = " << Neighbor->second->used << std::endl;
if ((*library).checkPrefix(word) && Neighbor->second->used == false)
{
// std::cout << "Word: " << word << " prefix = " << (*library).checkPrefix(word)<< std::endl;
do
{
this->traverse(Neighbor->second, word, library);
} while (Neighbor != cell->nextTo.end() && letter.compare(*(Neighbor->first)));
}
else
{
while (Neighbor != cell->nextTo.end() && letter.compare(*(Neighbor->first)))
{
++Neighbor;
std::cout << "Else: " << letter << " and " << (*(Neighbor->first)) << " evaluates to " << letter.compare(*(Neighbor->first)) << std::endl;
}
}
// std::cout << "Finished examining " << word << std::endl;
word.pop_back();
++Neighbor;
}
cell->used = false;
return;
}
/*************************************
Main:
Creates dictionary
Then creates cubes and traverses them
*************************************/
int main(int arc, char* argv[])
{
// Check usage
if (arc != 3)
{
std::cout << "usage: BoggleSolver cube-file word-file" << std::endl;
return 0;
}
// The clock is running
double duration;
std::clock_t start;
// Make new dictionary object
WordWizard* reference = new WordWizard;
reference->buildHash(argv[2]);
reference->buildSet(argv[2]);
// Iterate over list of cubes
// Upload cube
std::ifstream infile(argv[1]);
std::string s;
int cubeSize;
int cubeCount = 0;
// std::cout << "Stage has been set" << std::endl;
while (std::getline(infile, s))
{
try
{
cubeSize = (int) cbrt(s.length());
Cube game (cubeSize);
cubeCount++;
game.setConnections();
game.setLetters(s);
//game.printCube();
// std::cout << "Game has been set" << std::endl;
int wordCount = 0;
for (int i = 0; i < (pow(cubeSize, 3)); i++)
{
// std::cout << "Starting to traverse a new cubie: " << game.Cubies[i]->letter << std::endl;
std::string word = game.Cubies[i]->letter;
game.traverse(game.Cubies[i], word, reference);
}
std::cout << game.totalWords << std::endl;
game.garbage();
}
catch (int e)
{
std::cout << "Exception! Check each cube has the correct number of letters. Exception #" << e << std::endl;
}
}
// Print # of cubes and time
duration = (std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout << "Scored " << cubeCount << " cubes with dimension " << cubeSize << " in " << duration << " seconds." << std::endl;
}