Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace _307_intermediate {
  7. class ScrabbleProblemSolver {
  8.  
  9. static void Main(string[] args) {
  10. string path = (@"../../enable1.txt");
  11. string[] lines = File.ReadAllLines(path);
  12. List<string> longestChain = new List<string>();
  13.  
  14. var twoLetterWords = lines.Where(l => l.Length == 2);
  15.  
  16. foreach (var word in twoLetterWords) {
  17. List<string> currentChain = RecursiveCheck(word, lines, longestChain, new List<string>());
  18. if (currentChain.Count > longestChain.Count) {
  19. longestChain = currentChain;
  20. }
  21. }
  22.  
  23. if (longestChain.Count == 0) {
  24. Console.WriteLine("There don't seem to be any words in this file.");
  25. } else {
  26. Console.WriteLine("The longest word we can build using this method is {0} characters long.", longestChain.Last().Length);
  27. Console.WriteLine("It's {0}.", longestChain.Last());
  28. if (longestChain.Count > 1) {
  29. Console.WriteLine("Here's the full chain:");
  30. foreach (var word in longestChain) {
  31. Console.WriteLine("{0} - {1}", word.Length, word);
  32. }
  33. }
  34. }
  35.  
  36. Console.WriteLine("Press Enter to exit.");
  37. Console.ReadLine();
  38. }
  39.  
  40. static List<string> RecursiveCheck(string word, string[] words, List<string> longestChain, List<string> previousChain) {
  41. List<string> currentChain = new List<string>(previousChain);
  42. currentChain.Add(word);
  43. var longerWords = words.Where(w => w.Contains(word) && w.Length > word.Length).OrderBy(w => w.Length);
  44.  
  45. if (longerWords.Count() == 0) {
  46. return currentChain;
  47. }
  48. if (longerWords.Last().Length <= (longestChain.Count - 1)) {
  49. // no point in continuing to go down this route
  50. return currentChain;
  51. }
  52.  
  53. List<string> returnChain = new List<string>(currentChain);
  54.  
  55. foreach (var longerWord in longerWords.Where(w => w.Length == word.Length + 1)) {
  56. List<string> newChain = RecursiveCheck(longerWord, longerWords.ToArray(), longestChain, currentChain);
  57. if (newChain.Count > returnChain.Count) {
  58. returnChain = newChain;
  59. }
  60. }
  61. return returnChain;
  62. }
  63.  
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement