Advertisement
Guest User

ScrabbleCheck.cs

a guest
Jul 30th, 2020
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. // used in constructing answer at
  8. // https://puzzling.stackexchange.com/questions/100498/how-many-times-can-the-x-tile-be-used-in-one-scrabble-game/100572#100572
  9.  
  10. public class ScrabbleCheck
  11. {
  12.   static public void Main()
  13.   {
  14.     HashSet<string> ValidWords = new HashSet<string>(ReadFileLines("scrabble.txt").Select(l => l.ToUpperInvariant()));
  15.     Dictionary<string, List<List<string>>> chains = new Dictionary<string, List<List<string>>>();
  16.     foreach (var word in ValidWords.Where(l => l.Contains('X')).OrderBy(l => l.Length))
  17.     {
  18.       int bestChainLength = 0;
  19.       List<List<string>> bestChains = null;
  20.       foreach (var chain in chains)
  21.       {
  22.         if (word.Contains(chain.Key))
  23.         {
  24.           if (chain.Value[0].Count > bestChainLength)
  25.           {
  26.             bestChainLength = chain.Value.Count;
  27.             bestChains = chain.Value.Select(l => new List<string>(l) { word }).ToList();
  28.           }
  29.           else if (chain.Value[0].Count == bestChainLength)
  30.             bestChains.AddRange(chain.Value.Select(l => new List<string>(l) { word }));
  31.         }
  32.       }
  33.       chains.Add(word, bestChains ?? new List<List<string>> { new List<string> { word } });
  34.     }
  35.     foreach (var greatChain in chains.OrderByDescending(chain => chain.Value[0].Count).Take(5))
  36.     {
  37.       foreach(var path in greatChain.Value)
  38.         Console.WriteLine($"{path.Count}: " + string.Join(" => ", path));
  39.     }
  40.     ValidWords.Remove("DI");
  41.     foreach (var word in ValidWords)
  42.     {
  43.       if (word.Length == 7 && !word.Contains('S') &&
  44.          word[0] == 'A' &&
  45.          ValidWords.Contains(word[1] + "I") &&
  46.          ValidWords.Contains(word[2] + "M") &&
  47.          ValidWords.Contains(word[3] + "A") &&
  48.          ValidWords.Contains(word[4] + "L") &&
  49.          ValidWords.Contains(word[5] + "I"))
  50.         Console.WriteLine($"Word above: {word}");
  51.       else if (word.Length == 4 && word[0] == 'E' && word[1] == 'S')
  52.         Console.WriteLine("Word down: " + word);
  53.     }
  54.   }
  55.  
  56.  
  57.   public static IEnumerable<string> ReadFileLines(string inputFile)
  58.   {
  59.     using (var file = new StreamReader(inputFile))
  60.     {
  61.       while (!file.EndOfStream)
  62.         yield return file.ReadLine();
  63.     }
  64.   }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement