made it run parralell to speed it up and got rid of words bigger then 6 chars

This commit is contained in:
Nicolai Van der Storm 2024-07-23 14:48:44 +02:00
parent 4c9023445a
commit 93cbe5115e
9 changed files with 23 additions and 10 deletions

View File

@ -7,8 +7,13 @@
var wordSet = new HashSet<string>(words);
// Find all combinations that form a 6-letter word
var filteredWords = words.Where(w => w.Length <= 6).ToList();
var combos = new HashSet<string>();
FindCombinations(words, new List<string>(), 0, wordSet, combos);
Parallel.ForEach(filteredWords, word =>
{
FindCombinations(word, filteredWords, new List<string> { word }, word.Length, wordSet, combos);
});
// Print out all found combinations
foreach (var combo in combos)
@ -17,18 +22,21 @@
}
}
static void FindCombinations(List<string> words, List<string> currentCombination, int currentLength, HashSet<string> wordSet, HashSet<string> combos)
static void FindCombinations(string currentWord, List<string> words, List<string> currentCombination, int currentLength, HashSet<string> wordSet, HashSet<string> combos)
{
// If the current combination forms a 6-letter word, add it to the result list
// Check if the current combination forms a valid 6-letter word
if (currentLength == 6)
{
string combinedWord = string.Join("", currentCombination);
if (wordSet.Contains(combinedWord))
{
string result = $"{string.Join("+", currentCombination)}={combinedWord}";
if (combos.Add(result)) // Adds to the set and checks for uniqueness
lock (combos) // Ensure thread-safe access to combos
{
Console.WriteLine(result); // Echo out the found combination
if (combos.Add(result)) // Add to the set and check for uniqueness
{
Console.WriteLine(result); // Echo out the found combination
}
}
}
return;
@ -43,10 +51,15 @@
continue;
}
// Avoid duplicate combinations
if (currentCombination.Contains(word))
{
continue;
}
// Add the word to the current combination and recurse
currentCombination.Add(word);
FindCombinations(words, currentCombination, currentLength + word.Length, wordSet, combos);
currentCombination.RemoveAt(currentCombination.Count - 1); // Backtrack
var newCombination = new List<string>(currentCombination) { word };
FindCombinations(currentWord, words, newCombination, currentLength + word.Length, wordSet, combos);
}
}
}

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("6letterwordexercise")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d71eb795d7a7de915bd319b60249ee90f2bc5538")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4c9023445a69c66dd72633b718cbef8565efa4c6")]
[assembly: System.Reflection.AssemblyProductAttribute("6letterwordexercise")]
[assembly: System.Reflection.AssemblyTitleAttribute("6letterwordexercise")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
68c131bf4bbdc8c6ee3c840269c62122d679fae9c2f5cb638dc03527d6749d88
1a32798c3322f35ba2923c1415f22677216d6d303f9a0a3678dd3e3d2ec1deba