Skip to content

Instantly share code, notes, and snippets.

@MindStudioOfficial
Created August 8, 2022 13:28
Show Gist options
  • Save MindStudioOfficial/cdefd6559aeb34e6081f86ea31154543 to your computer and use it in GitHub Desktop.
Save MindStudioOfficial/cdefd6559aeb34e6081f86ea31154543 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <omp.h>
#include <bitset>
#include <algorithm>
using namespace std;
int mapToInt(string s)
{
int out = 0;
for (int i = 0; i < s.length(); i++)
{
out |= 1 << 26 >> (uint8_t)s[i];
}
return out;
}
vector<string> words;
string decodeWord(int rep)
{
string res = "";
#pragma omp parallel for num_threads(32)
for (int i = 0; i < words.size(); i++)
{
if (mapToInt(words[i]) == rep)
res += words[i] + "/";
}
return res.substr(0, res.length() - 1);
}
int main()
{
fstream file;
file.open("wordle-nyt-allowed-guesses.txt", fstream::in);
if (file.is_open())
{
string s;
while (getline(file, s))
{
words.push_back(s);
}
file.close();
}
file.open("wordle-nyt-answers-alphabetical.txt", fstream::in);
if (file.is_open())
{
string s;
while (getline(file, s))
{
words.push_back(s);
}
file.close();
}
int wordcount = (int)words.size();
printf("Found %d words\n", wordcount);
vector<int> intReps;
for (int i = 0; i < wordcount; i++)
{
int intRep = mapToInt(words[i]);
bitset<32> b(intRep);
if (b.count() == 5 && find(intReps.begin(), intReps.end(), intRep) == intReps.end())
{
intReps.push_back(intRep);
}
}
sort(intReps.begin(),intReps.end());
int filteredWordCount = (int)intReps.size();
int* firstStep = (int*)calloc(filteredWordCount,sizeof(int));
#pragma omp parallel for num_threads(32)
for (int i = 0; i < filteredWordCount; ++i)
{
int A = intReps[i];
int j;
for (j = i + 1; j < filteredWordCount; ++j)
{
int B = intReps[j];
if ((A & B) == 0)
break;
}
firstStep[i] = j - i;
}
printf("Found %d filtered words\n", filteredWordCount);
#pragma omp parallel for num_threads(32)
for (int i = 0; i < filteredWordCount; i++)
{
int A = intReps[i];
for (int j = i + firstStep[i]; j < filteredWordCount; ++j)
{
int B = intReps[j];
if ((A & B) != 0)
continue;
int AB = A | B;
for (int k = j + firstStep[j]; k < filteredWordCount; ++k)
{
int C = intReps[k];
if ((AB & C) != 0)
continue;
int ABC = AB | C;
for (int l = k + firstStep[k]; l < filteredWordCount; ++l)
{
int D = intReps[l];
if ((ABC & D) != 0)
continue;
int ABCD = ABC | D;
for (int m = l + firstStep[l]; m < filteredWordCount; ++m)
{
int E = intReps[m];
if ((ABCD & E) != 0)
continue;
printf("%d Words:\n%s\n%s\n%s\n%s\n%s\n\n",
i,
decodeWord(A).c_str(),
decodeWord(B).c_str(),
decodeWord(C).c_str(),
decodeWord(D).c_str(),
decodeWord(E).c_str());
}
}
}
}
}
free(firstStep);
return 0;
}
@tigerplush
Copy link

I replied in another gist:

maybe don't use a vector or set the vectors capacity at start so it only allocates memory once 🤔

because vector::push_back allocates new memory if the capacity is exceeded and for adding 13k words you do 13k allocations
https://cplusplus.com/reference/vector/vector/push_back/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment