Compare commits
No commits in common. "slow_algo" and "main" have entirely different histories.
75
Program.cs
75
Program.cs
@ -1,65 +1,24 @@
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
// Read all words from the input.txt file and store them in a hash set for quick lookup
|
||||
class Program {
|
||||
static void Main() {
|
||||
// get all the words form the text file and put them into a hasset
|
||||
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>();
|
||||
// Find all combos that forn a 6 letter word and make sure we dont have duplicates
|
||||
var seenCombinations = new HashSet<string>();
|
||||
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();
|
||||
|
||||
Parallel.ForEach(filteredWords, word =>
|
||||
{
|
||||
FindCombinations(word, filteredWords, new List<string> { word }, word.Length, 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(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
@ -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+4c9023445a69c66dd72633b718cbef8565efa4c6")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7a30bacb68910559e2c2bc33337d57c1364a1b60")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("6letterwordexercise")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("6letterwordexercise")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
@ -1 +1 @@
|
||||
1a32798c3322f35ba2923c1415f22677216d6d303f9a0a3678dd3e3d2ec1deba
|
||||
38d39cd6b0bba17b8e33dc8144efa9e3646f6745189f6a226aa41ca2ccf216ba
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user