Skip to content

Commit

Permalink
switch to ints for colorings if applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
gwstaten committed Jul 10, 2022
1 parent aba92ac commit 079e409
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool inputWordSet(std::vector<std::string> &wordSet, unsigned int correctSize);
int countDistinct(std::string s);

std::string grade(std::string guess, std::string answer);
unsigned long long int grade2(std::string guess, std::string answer);
std::vector<std::string> filter(std::vector<std::string> wordList, std::pair<std::string, std::string> filter);
std::vector<std::string> filterHM(std::vector<std::string> wordList, std::pair<std::string, std::string> filter);
std::vector<std::vector<std::string>> SplitVector(const std::vector<std::string>& vec, size_t n);
Expand Down
3 changes: 0 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,6 @@ int main(int argc, char* argv[])
}
}

std::sort(validWords.begin(), validWords.end());
std::sort(validWordss.begin(), validWordss.end());

if(!searchMode)
{
std::cout << "Search mode (1 - 6)? ";
Expand Down
81 changes: 80 additions & 1 deletion src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,58 @@ double rate(std::vector<std::string> guess, std::vector<std::string> words, int
return total;
}

double rateInts(std::vector<std::string> guess, std::vector<std::string> words, int searchMode, std::vector<unsigned long long int> prefixColorings)
{
unsigned long long int factor = std::pow(3,guess[0].size());
std::unordered_map<unsigned long long int, double> ratingsMap;
for(unsigned int answer = 0; answer < words.size(); answer++)
{
unsigned long long int total = prefixColorings[answer];
for(unsigned int i = 0; i < guess.size(); i++)
{
total *= factor;
total += grade2(guess[i], words[answer]);
}
ratingsMap[total]++;
}
if(searchMode == 2)
{
return ratingsMap.size();
}
double total = 0;
std::unordered_map<unsigned long long int, double>::iterator it;
for(it = ratingsMap.begin(); it != ratingsMap.end(); ++it)
{
std::vector<bool> usedGreen(guess[0].length(),false);
switch(searchMode)
{
case 1:
total += ((it->second) * (it->second));
break;
case 3:
if((it->second) == 1)
{
total++;
}
break;
case 4:
if(total < (it->second))
{
total = (it->second);
}
break;
case 5:
total += (it->second) * std::log((it->second) / words.size()) / std::log(0.5);
break;
}
}
if(searchMode == 1 || searchMode == 5 || searchMode == 6)
{
total /= words.size();
}
return total;
}

void findBestThread(std::vector<std::string> words, std::vector<std::string> validWords, std::vector<std::pair<double,std::string>> &out, int searchMode, std::vector<std::string> prefix, std::vector<std::string> allguess, int setsize, int unique, bool newBestPrints, int threadNum, std::string forceInclude, std::vector<int> uniqueSteps, int updatePrintFrequency, std::string keyword, int fullRankingRequiredScore, std::vector<std::string> forceIncludePos, bool cont)
{
std::ofstream fout;
Expand Down Expand Up @@ -291,6 +343,9 @@ void findBestThread(std::vector<std::string> words, std::vector<std::string> val
fout.close();
}
}

bool intColorings = (words[0].size() <= 11 && searchMode != 6 && words[0].size() * (prefix.size() + setsize) <= 40);

std::vector<std::string> prefixColorings = {};
for(unsigned int i = 0; i < words.size(); i++)
{
Expand All @@ -301,6 +356,22 @@ void findBestThread(std::vector<std::string> words, std::vector<std::string> val
}
prefixColorings.push_back(prefixColoring);
}
std::vector<unsigned long long int> prefixColorings2 = {};
if(intColorings)
{
unsigned long long int factor = std::pow(3,words[0].size());
for(unsigned int i = 0; i < words.size(); i++)
{
unsigned long long int prefixColor2 = 0;
for(unsigned int j = 0; j < prefix.size(); j++)
{
prefixColor2 *= factor;
prefixColor2 += grade2(prefix[j], words[i]);
}
prefixColorings2.push_back(prefixColor2);
}
}

std::string prefixStarter = "";
if(prefix.size())
{
Expand Down Expand Up @@ -434,7 +505,15 @@ void findBestThread(std::vector<std::string> words, std::vector<std::string> val
}
if(stillGood)
{
double total = rate(guessVec, words, searchMode, prefixColorings);
double total;
if(intColorings)
{
total = rateInts(guessVec, words, searchMode, prefixColorings2);
}
else
{
total = rate(guessVec, words, searchMode, prefixColorings);
}
numberChecked++;
if(first || (total < best && (searchMode == 1 || searchMode == 4)) || (total > best && !(searchMode == 1 || searchMode == 4)))
{
Expand Down
33 changes: 33 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,39 @@ int countDistinct(std::string s)
return m.size();
}

unsigned long long int positions[] = {1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441};

unsigned long long int grade2(std::string guess, std::string answer)
{
const unsigned int size = guess.length();
unsigned long long int out = 0;
std::vector<bool> usedAnswer(size,false);
for(unsigned int i = 0; i < size; i++)
{
if(guess[i] == answer.at(i))
{
out += 2 * positions[i];
usedAnswer[i] = true;
}
}
for(unsigned int guessSpot = 0; guessSpot < size; guessSpot++)
{
if((out / positions[guessSpot]) % 3 == 0)
{
bool stillSearching = true;
for(unsigned int answerSpot = 0; answerSpot < size && stillSearching; answerSpot++)
{
if(!usedAnswer[answerSpot] && guess[guessSpot] == answer[answerSpot])
{
out += positions[guessSpot];
usedAnswer[answerSpot] = true;
stillSearching = false;
}
}
}
}
return out;
}

std::string grade(std::string guess, std::string answer)
{
Expand Down

0 comments on commit 079e409

Please sign in to comment.