Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- // used in constructing answer at
- // https://puzzling.stackexchange.com/questions/100498/how-many-times-can-the-x-tile-be-used-in-one-scrabble-game/100572#100572
- public class ScrabbleCheck
- {
- static public void Main()
- {
- HashSet<string> ValidWords = new HashSet<string>(ReadFileLines("scrabble.txt").Select(l => l.ToUpperInvariant()));
- Dictionary<string, List<List<string>>> chains = new Dictionary<string, List<List<string>>>();
- foreach (var word in ValidWords.Where(l => l.Contains('X')).OrderBy(l => l.Length))
- {
- int bestChainLength = 0;
- List<List<string>> bestChains = null;
- foreach (var chain in chains)
- {
- if (word.Contains(chain.Key))
- {
- if (chain.Value[0].Count > bestChainLength)
- {
- bestChainLength = chain.Value.Count;
- bestChains = chain.Value.Select(l => new List<string>(l) { word }).ToList();
- }
- else if (chain.Value[0].Count == bestChainLength)
- bestChains.AddRange(chain.Value.Select(l => new List<string>(l) { word }));
- }
- }
- chains.Add(word, bestChains ?? new List<List<string>> { new List<string> { word } });
- }
- foreach (var greatChain in chains.OrderByDescending(chain => chain.Value[0].Count).Take(5))
- {
- foreach(var path in greatChain.Value)
- Console.WriteLine($"{path.Count}: " + string.Join(" => ", path));
- }
- ValidWords.Remove("DI");
- foreach (var word in ValidWords)
- {
- if (word.Length == 7 && !word.Contains('S') &&
- word[0] == 'A' &&
- ValidWords.Contains(word[1] + "I") &&
- ValidWords.Contains(word[2] + "M") &&
- ValidWords.Contains(word[3] + "A") &&
- ValidWords.Contains(word[4] + "L") &&
- ValidWords.Contains(word[5] + "I"))
- Console.WriteLine($"Word above: {word}");
- else if (word.Length == 4 && word[0] == 'E' && word[1] == 'S')
- Console.WriteLine("Word down: " + word);
- }
- }
- public static IEnumerable<string> ReadFileLines(string inputFile)
- {
- using (var file = new StreamReader(inputFile))
- {
- while (!file.EndOfStream)
- yield return file.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement