Skip to content

Instantly share code, notes, and snippets.

@escalonn
Last active April 15, 2022 19:43
Show Gist options
  • Save escalonn/e5fc64487b621d46f84eceba35fa3439 to your computer and use it in GitHub Desktop.
Save escalonn/e5fc64487b621d46f84eceba35fa3439 to your computer and use it in GitHub Desktop.
// 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