Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8.  
  9. namespace Wordlist {
  10. public class Program {
  11. public static void Main(string[] args) {
  12. IList<IWord> words = ReadWords().ToList();
  13. var stopwatch = new Stopwatch();
  14. stopwatch.Start();
  15. const int requestedLength = 6;
  16. // index all words by their length
  17. ILookup<int, IWord> wordsByLength = words.ToLookup(w => w.Length);
  18. // create a set of all words of the desired total length. This makes "Contains" checks blazing fast
  19. var wordEqualityComparer = new WordEqualityComparer();
  20. ISet<IWord> wordsOfRequestedLength = new HashSet<IWord>(wordsByLength[requestedLength], wordEqualityComparer);
  21.  
  22. var combinations = Enumerable.Range(1, requestedLength - 1)
  23. // start with all words of length "i"
  24. .SelectMany(i => wordsByLength[i]
  25. // create a combination with all other words of length "total - i"
  26. .SelectMany(word => wordsByLength[requestedLength - i].Select(otherWord => new WordCombination(word, otherWord)))
  27. // verify that combination is in the list
  28. .Where(combination => wordsOfRequestedLength.Contains(combination) )
  29. )
  30. // materialize the results
  31. .ToList();
  32.  
  33. stopwatch.Stop();
  34.  
  35. Console.WriteLine("Combinations: " + combinations.Count);
  36. Console.WriteLine("Time taken: " + stopwatch.ElapsedMilliseconds + "ms");
  37. Console.WriteLine("Press [enter] to see all combinations");
  38. Console.ReadLine();
  39. Console.WriteLine(string.Join(Environment.NewLine, combinations));
  40. Console.ReadLine();
  41. }
  42.  
  43.  
  44. private static IEnumerable<IWord> ReadWords() {
  45. var assembly = Assembly.GetExecutingAssembly();
  46. var resourceName = "Wordlist.wordlist.txt";
  47.  
  48. using (var stream = assembly.GetManifestResourceStream(resourceName)) {
  49. using (var reader = new StreamReader(stream, Encoding.UTF8)) {
  50. string line;
  51. while ((line = reader.ReadLine()) != null) {
  52. yield return new Word(line);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59.  
  60. namespace Wordlist {
  61. public interface IWord {
  62. string Content { get; }
  63. int Length { get; }
  64. }
  65. }
  66.  
  67. namespace Wordlist {
  68. public class Word : IWord {
  69. public string Content { get; }
  70. public int Length { get; }
  71.  
  72. public Word(string content) {
  73. Content = content;
  74. Length = content.Length;
  75. }
  76. }
  77. }
  78. namespace Wordlist {
  79. public class WordCombination : IWord {
  80. private IWord Left { get; }
  81. private IWord Right { get; }
  82. public int Length { get; }
  83. public string Content { get; }
  84.  
  85. public WordCombination(IWord left, IWord right) {
  86. Left = left;
  87. Right = right;
  88. Content = left.Content + right.Content;
  89. Length = Content.Length;
  90. }
  91.  
  92. public override string ToString() {
  93. return $"{Left.Content} + {Right.Content} = {Content}";
  94. }
  95. }
  96. }
  97.  
  98. using System;
  99. using System.Collections.Generic;
  100.  
  101. namespace Wordlist {
  102. public class WordEqualityComparer : IEqualityComparer<IWord> {
  103. public bool Equals(IWord x, IWord y) {
  104. return x.Length == y.Length && string.Equals(x.Content, y.Content, StringComparison.OrdinalIgnoreCase);
  105. }
  106.  
  107. public int GetHashCode(IWord word) {
  108. return word.Content.GetHashCode();
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement