Skip to content

Commit

Permalink
optimization for prefix searches
Browse files Browse the repository at this point in the history
  • Loading branch information
gwstaten committed Jul 10, 2022
1 parent 7a34d5d commit 46cfb72
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion include/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ std::vector<std::vector<std::string>> SplitVector(const std::vector<std::string>
int countDistinct(std::string s);
bool inputWordSet(std::vector<std::string> &wordSet, unsigned int correctSize);

double rate(std::vector<std::string> guess, std::vector<std::string> words, int searchMode);
double rate(std::vector<std::string> guess, std::vector<std::string> words, int searchMode, std::vector<std::string> prefixColorings);
void findbest(std::vector<std::string> valids, std::vector<std::string> validGuesses, int numThreads, int searchMode, std::vector<std::string> prefix, bool fullRankingOut, int fullRankingRequiredScore, int setSize, int unique, bool newBestPrints, std::string forceInclude, std::string forceExclude, std::vector<int> uniqueSteps, int updatePrintFrequency, std::string wordlist, std::vector<std::string> forceExcludePos, std::vector<std::string> forceIncludePos, bool answersOnlyFirst, std::string keyword);
void findbest(std::string keyword);
#endif
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ int main(int argc, char* argv[])
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
return word;
});
std::vector<std::string> blank(valids.size(),"");

if(wordSet.size() > 0 || inputWordSet(std::ref(wordSet), valids[0].length()))
{
Expand All @@ -420,7 +421,7 @@ int main(int argc, char* argv[])
tempWordSet.push_back(wordSet[k]);
}
}
double score = rate(tempWordSet, valids, u);
double score = rate(tempWordSet, valids, u, blank);
if(!j || (score < bestScore && (u == 1 || u == 4)) || (score > bestScore && !(u == 1 || u == 4)))
{
bestScore = score;
Expand Down
47 changes: 26 additions & 21 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ void rateAll(std::vector<std::string> guess, std::vector<std::string> words, std
std::cout << std::endl;
}

double rate(std::vector<std::string> guess, std::vector<std::string> words, int searchMode)
double rate(std::vector<std::string> guess, std::vector<std::string> words, int searchMode, std::vector<std::string> prefixColorings)
{
std::unordered_map<std::string, double> ratingsMap;
for(unsigned int answer = 0; answer < words.size(); answer++)
{
std::string total = "";
std::string total = prefixColorings[answer];
for(unsigned int i = 0; i < guess.size(); i++)
{
total += grade(guess[i], words[answer]);
Expand Down Expand Up @@ -291,6 +291,27 @@ void findBestThread(std::vector<std::string> words, std::vector<std::string> val
fout.close();
}
}
std::vector<std::string> prefixColorings = {};
for(unsigned int i = 0; i < words.size(); i++)
{
std::string prefixColoring = "";
for(unsigned int j = 0; j < prefix.size(); j++)
{
prefixColoring += grade(prefix[j], words[i]);
}
prefixColorings.push_back(prefixColoring);
}
std::string prefixStarter = "";
for(unsigned int i = 0; i < prefix.size(); i++)
{
if(i)
{
prefixStarter += "-";
}
prefixStarter += prefix[i];
}
prefixStarter += "-";

auto startTime = std::chrono::system_clock::now();
for(unsigned int guess = 0; notdone; guess++)
{
Expand Down Expand Up @@ -322,24 +343,8 @@ void findBestThread(std::vector<std::string> words, std::vector<std::string> val
}
int toIncrement = setsize - 1;

std::vector<std::string> guessVec = prefix;
std::string comb;
if(prefix.size())
{
for(unsigned int i = 0; i < prefix.size(); i++)
{
if(i)
{
comb += "-";
}
comb += prefix[i];
}
comb += "+" + validWords[positions[0]];
}
else
{
comb = validWords[positions[0]];
}
std::vector<std::string> guessVec;
std::string comb = prefixStarter + validWords[positions[0]];
guessVec.push_back(validWords[positions[0]]);
std::string prior = comb;
bool alpha = true;
Expand Down Expand Up @@ -426,7 +431,7 @@ void findBestThread(std::vector<std::string> words, std::vector<std::string> val
}
if(stillGood)
{
double total = rate(guessVec, words, searchMode);
double total = rate(guessVec, words, searchMode, prefixColorings);
numberChecked++;
if(first || (total < best && (searchMode == 1 || searchMode == 4)) || (total > best && !(searchMode == 1 || searchMode == 4)))
{
Expand Down

0 comments on commit 46cfb72

Please sign in to comment.