diff --git a/Program.cs b/Program.cs index d2e46fe..9bc45df 100644 --- a/Program.cs +++ b/Program.cs @@ -7,8 +7,13 @@ var wordSet = new HashSet(words); // Find all combinations that form a 6-letter word + var filteredWords = words.Where(w => w.Length <= 6).ToList(); var combos = new HashSet(); - FindCombinations(words, new List(), 0, wordSet, combos); + + Parallel.ForEach(filteredWords, word => + { + FindCombinations(word, filteredWords, new List { word }, word.Length, wordSet, combos); + }); // Print out all found combinations foreach (var combo in combos) @@ -17,18 +22,21 @@ } } - static void FindCombinations(List words, List currentCombination, int currentLength, HashSet wordSet, HashSet combos) + static void FindCombinations(string currentWord, List words, List currentCombination, int currentLength, HashSet wordSet, HashSet 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(currentCombination) { word }; + FindCombinations(currentWord, words, newCombination, currentLength + word.Length, wordSet, combos); } } } diff --git a/bin/Debug/net8.0/6letterwordexercise.dll b/bin/Debug/net8.0/6letterwordexercise.dll index f6e3f43..d05c003 100644 Binary files a/bin/Debug/net8.0/6letterwordexercise.dll and b/bin/Debug/net8.0/6letterwordexercise.dll differ diff --git a/bin/Debug/net8.0/6letterwordexercise.pdb b/bin/Debug/net8.0/6letterwordexercise.pdb index e5d9de2..6bd686a 100644 Binary files a/bin/Debug/net8.0/6letterwordexercise.pdb and b/bin/Debug/net8.0/6letterwordexercise.pdb differ diff --git a/obj/Debug/net8.0/6letterwordexercise.AssemblyInfo.cs b/obj/Debug/net8.0/6letterwordexercise.AssemblyInfo.cs index 62691fc..9f99bb0 100644 --- a/obj/Debug/net8.0/6letterwordexercise.AssemblyInfo.cs +++ b/obj/Debug/net8.0/6letterwordexercise.AssemblyInfo.cs @@ -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")] diff --git a/obj/Debug/net8.0/6letterwordexercise.AssemblyInfoInputs.cache b/obj/Debug/net8.0/6letterwordexercise.AssemblyInfoInputs.cache index ce85501..7b05234 100644 --- a/obj/Debug/net8.0/6letterwordexercise.AssemblyInfoInputs.cache +++ b/obj/Debug/net8.0/6letterwordexercise.AssemblyInfoInputs.cache @@ -1 +1 @@ -68c131bf4bbdc8c6ee3c840269c62122d679fae9c2f5cb638dc03527d6749d88 +1a32798c3322f35ba2923c1415f22677216d6d303f9a0a3678dd3e3d2ec1deba diff --git a/obj/Debug/net8.0/6letterwordexercise.dll b/obj/Debug/net8.0/6letterwordexercise.dll index f6e3f43..d05c003 100644 Binary files a/obj/Debug/net8.0/6letterwordexercise.dll and b/obj/Debug/net8.0/6letterwordexercise.dll differ diff --git a/obj/Debug/net8.0/6letterwordexercise.pdb b/obj/Debug/net8.0/6letterwordexercise.pdb index e5d9de2..6bd686a 100644 Binary files a/obj/Debug/net8.0/6letterwordexercise.pdb and b/obj/Debug/net8.0/6letterwordexercise.pdb differ diff --git a/obj/Debug/net8.0/ref/6letterwordexercise.dll b/obj/Debug/net8.0/ref/6letterwordexercise.dll index 618bb87..c6e8d7e 100644 Binary files a/obj/Debug/net8.0/ref/6letterwordexercise.dll and b/obj/Debug/net8.0/ref/6letterwordexercise.dll differ diff --git a/obj/Debug/net8.0/refint/6letterwordexercise.dll b/obj/Debug/net8.0/refint/6letterwordexercise.dll index 618bb87..c6e8d7e 100644 Binary files a/obj/Debug/net8.0/refint/6letterwordexercise.dll and b/obj/Debug/net8.0/refint/6letterwordexercise.dll differ