Optimization

rx
Inga 🏳‍🌈 8 years ago
parent cbe7344681
commit 30a55106cf
  1. 30
      WhiteRabbit/Processor.cs

@ -38,9 +38,10 @@
public IEnumerable<string> GeneratePhrases(IEnumerable<string> words) public IEnumerable<string> GeneratePhrases(IEnumerable<string> words)
{ {
var filtered = FilterWords(words); var filtered = FilterWords(words);
var formattedWords = FormatWords(filtered); var formattedWordsList = FormatWords(filtered);
var formattedWords = formattedWordsList.ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2);
var dictionary = ImmutableStack.Create(formattedWords.Keys.ToArray()); var dictionary = ImmutableStack.Create(formattedWordsList.Select(tuple => tuple.Item1).ToArray());
var anagrams = GenerateOrderedPhrases(this.NumberOfOccurrences, ImmutableStack.Create<int[]>(), dictionary); var anagrams = GenerateOrderedPhrases(this.NumberOfOccurrences, ImmutableStack.Create<int[]>(), dictionary);
var anagramsWords = anagrams var anagramsWords = anagrams
.Select(list => ImmutableStack.Create(list.Select(wordArray => formattedWords[wordArray]).ToArray())) .Select(list => ImmutableStack.Create(list.Select(wordArray => formattedWords[wordArray]).ToArray()))
@ -59,13 +60,30 @@
.Where(word => word.GroupBy(ch => this.CharToInt[ch]).All(group => group.Count() <= this.NumberOfOccurrences[group.Key])); .Where(word => word.GroupBy(ch => this.CharToInt[ch]).All(group => group.Count() <= this.NumberOfOccurrences[group.Key]));
} }
private Dictionary<int[], string[]> FormatWords(IEnumerable<string> filteredWords) private int GetWordWeight(int[] word)
{
var result = 0;
var orderedChars = Enumerable.Range(0, DifferentChars)
.Select(i => new { Index = i, Count = this.NumberOfOccurrences[i] })
.OrderBy(charInfo => charInfo.Count)
.ThenBy(charInfo => charInfo.Index);
foreach (var charInfo in orderedChars)
{
result += word[charInfo.Index];
result *= charInfo.Count + 1;
}
return result;
}
private List<Tuple<int[], string[]>> FormatWords(IEnumerable<string> filteredWords)
{ {
return filteredWords return filteredWords
.GroupBy(word => new string(word.OrderBy(ch => ch).ToArray())) .GroupBy(word => new string(word.OrderBy(ch => ch).ToArray()))
.ToDictionary( .Select(group => Tuple.Create(Enumerable.Range(0, DifferentChars).Select(i => group.Key.Count(ch => ch == IntToChar[i])).ToArray(), group.ToArray()))
group => Enumerable.Range(0, DifferentChars).Select(i => group.Key.Count(ch => ch == IntToChar[i])).ToArray(), .OrderBy(tuple => GetWordWeight(tuple.Item1)) //so that letters that are more rare will come first
group => group.ToArray()); .ToList();
} }
private int[] GetStatus(int[] originalState, int[] newWord, out int status) private int[] GetStatus(int[] originalState, int[] newWord, out int status)

Loading…
Cancel
Save