66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
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<string>(File.ReadAllLines("input.txt"));
|
|
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>();
|
|
|
|
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)
|
|
{
|
|
Console.WriteLine(combo);
|
|
}
|
|
}
|
|
|
|
static void FindCombinations(string currentWord, List<string> words, List<string> currentCombination, int currentLength, HashSet<string> wordSet, HashSet<string> combos)
|
|
{
|
|
// 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}";
|
|
lock (combos) // Ensure thread-safe access to combos
|
|
{
|
|
if (combos.Add(result)) // Add to the set and check 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;
|
|
}
|
|
|
|
// Avoid duplicate combinations
|
|
if (currentCombination.Contains(word))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Add the word to the current combination and recurse
|
|
var newCombination = new List<string>(currentCombination) { word };
|
|
FindCombinations(currentWord, words, newCombination, currentLength + word.Length, wordSet, combos);
|
|
}
|
|
}
|
|
}
|