Skip to content

Instantly share code, notes, and snippets.

@BraedonWooding
Last active August 17, 2022 10:44
Show Gist options
  • Save BraedonWooding/dc02de72b395a2155b8cce2a65686908 to your computer and use it in GitHub Desktop.
Save BraedonWooding/dc02de72b395a2155b8cce2a65686908 to your computer and use it in GitHub Desktop.
Finds 5 unique words that have unique letters
/*
Can you find: five five-letter words with twenty-five unique letters?
FJORD
GUCKS
NYMPH
VIBEX
WALTZ
Q
Similar solution to https://gist.github.com/fredoverflow/a7c9f230d86787ed6164ef652f675d8d
but is more generic (you can decrease words/increase them and play around with threads)
for use in graphs/other things.
Also seems to run around ~2x-3x faster,
I can process all the items using 32 threads in 2.400s (averaged over many runs) compared to 7.727s (also averaged) for the linked version above.
The user time (which is total threads time approximately) seems to also be better at 25.473s compared to 145.137s.
Half a minute with no threading is pretty nice.
Constraints:
- No duplicate letters (valid words have 5 unique characters)
- Order of letters irrelevant (ignore anagrams during search)
*/
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <unordered_map>
#include <bit>
#include <functional>
#include <thread>
#include <execution>
const int MaxCombinations = 5;
const int Threads = 32;
std::array<std::array<unsigned int, MaxCombinations>, Threads> wordsChosen = {};
size_t numOfWords = 0;
std::unordered_map<unsigned int, std::string> words_seen;
std::vector<unsigned int> words;
std::atomic_int counter = 0;
auto recurse(int offset, size_t start, size_t max, size_t threadNum, unsigned int value) -> void
{
if (offset >= MaxCombinations)
{
// we've found a full combination list, convert back to ascii
std::cout << "Found word list: ";
counter++;
for (int i = 0; i < MaxCombinations; i++)
{
std::cout << words_seen[wordsChosen[threadNum][i]] << " ";
}
std::cout << std::endl;
}
for (size_t i = start; i < max; i++)
{
unsigned int word = words[i];
// i.e. first should be 5, then 10, ..., then 25
if ((word & value) == 0)
{
wordsChosen[threadNum][offset] = word;
recurse(offset + 1, i + 1, numOfWords, threadNum, value | word);
}
}
}
int main(int argc, char *argv[])
{
// read the files in and convert
std::ifstream file;
file.open("./combined-words.txt");
std::string line;
int wordCount = 0;
while (!file.eof())
{
getline(file, line);
wordCount++;
unsigned int item = 0;
for (auto c : line)
{
if (c >= 'a' && c <= 'z')
{
item |= 1 << (c - 'a');
}
}
if (std::popcount(item) == 5 && !words_seen.contains(item))
{
words_seen[item] = line;
words.push_back(item);
}
}
std::cout << wordCount << " Total words" << std::endl;
std::cout << words_seen.size() << " Unique pruned words" << std::endl;
numOfWords = words.size();
// sorting eliminates a lot of similar words
// maybe sorting it differently could provide better results but eh
std::sort(std::execution::par_unseq, std::begin(words), std::end(words));
std::array<std::thread, Threads> threads = {};
for (int i = 0; i < Threads; i++)
{
std::thread thread_object(recurse, 0, (int)((double)(numOfWords * i) / Threads), (int)((double)(numOfWords * i) / Threads) + numOfWords / Threads, i, 0);
threads[i] = std::move(thread_object);
}
for (int i = 0; i < Threads; i++)
{
threads[i].join();
}
std::cout << counter << " unique word combinations" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment