diff --git a/Program.cs b/Program.cs index a56ce0e..d2e46fe 100644 --- a/Program.cs +++ b/Program.cs @@ -1,24 +1,52 @@ -class Program { - static void Main() { - // get all the words form the text file and put them into a hasset +class Program +{ + static void Main() + { + // Read all words from the input.txt file and store them in a hash set for quick lookup var words = new List(File.ReadAllLines("input.txt")); var wordSet = new HashSet(words); - // Find all combos that forn a 6 letter word and make sure we dont have duplicates - var seenCombinations = new HashSet(); - var combos = words.SelectMany( - word1 => words.Where(word2 => word1 != word2), - (word1, word2) => new { word1, word2, combinedWord = word1 + word2 } - ).Where( - x => x.combinedWord.Length == 6 && wordSet.Contains(x.combinedWord) - ).Where( - x => seenCombinations.Add($"{x.word1}+{x.word2}") - ).Select( - x => $"{x.word1}+{x.word2}={x.combinedWord}" - ).ToList(); + // Find all combinations that form a 6-letter word + var combos = new HashSet(); + FindCombinations(words, new List(), 0, wordSet, combos); - // echo out all found words - combos.ForEach(Console.WriteLine); + // Print out all found combinations + foreach (var combo in combos) + { + Console.WriteLine(combo); + } + } + static void FindCombinations(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 + 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 + { + Console.WriteLine(result); // Echo out the found combination + } + } + return; + } + + // Try adding each word to the current combination + foreach (var word in words) + { + // Skip if adding the word would exceed 6 characters + if (currentLength + word.Length > 6) + { + 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 + } } } diff --git a/bin/Debug/net8.0/6letterwordexercise.dll b/bin/Debug/net8.0/6letterwordexercise.dll index 1a70504..f6e3f43 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 3179971..e5d9de2 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 87ce2d2..62691fc 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+7a30bacb68910559e2c2bc33337d57c1364a1b60")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d71eb795d7a7de915bd319b60249ee90f2bc5538")] [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 0fa7ea7..ce85501 100644 --- a/obj/Debug/net8.0/6letterwordexercise.AssemblyInfoInputs.cache +++ b/obj/Debug/net8.0/6letterwordexercise.AssemblyInfoInputs.cache @@ -1 +1 @@ -38d39cd6b0bba17b8e33dc8144efa9e3646f6745189f6a226aa41ca2ccf216ba +68c131bf4bbdc8c6ee3c840269c62122d679fae9c2f5cb638dc03527d6749d88 diff --git a/obj/Debug/net8.0/6letterwordexercise.dll b/obj/Debug/net8.0/6letterwordexercise.dll index 1a70504..f6e3f43 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 3179971..e5d9de2 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 0aebd4d..618bb87 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 0aebd4d..618bb87 100644 Binary files a/obj/Debug/net8.0/refint/6letterwordexercise.dll and b/obj/Debug/net8.0/refint/6letterwordexercise.dll differ