Last active
April 15, 2022 19:43
-
-
Save escalonn/e5fc64487b621d46f84eceba35fa3439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example: dotnet run -- words_alpha.txt cat" | |
string dictionary = args[0]; | |
string tiles = args[1]; | |
Dictionary<string, List<string>> groups = File.ReadLines(path: dictionary) | |
.GroupBy(Alphabetize) | |
.ToDictionary(g => g.Key, g => g.ToList()); | |
IEnumerable<List<string>> matches = PowerSet(tiles) | |
.Select(Alphabetize) | |
.Where(groups.ContainsKey) | |
.OrderByDescending(s => s.Length) | |
.Select(s => groups[s]); | |
foreach (List<string> group in matches) | |
Console.WriteLine(string.Join(separator: ", ", values: group)); | |
static IEnumerable<IEnumerable<T>> PowerSet<T>(IEnumerable<T> set) => | |
Enumerable.Range(0, (int)Math.Pow(2, set.Count())) | |
.Select(b => set.Where((x, i) => ((1 << i) & b) != 0)); | |
static string Alphabetize(IEnumerable<char> x) => | |
string.Concat(x.OrderBy(c => c)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment