namespace AdventOfCode; public static class EnumerableExtensions { public static IEnumerable Flatten(this TSource root, Func> flattener) { var toVisit = new Stack(); toVisit.Push(root); while (toVisit.TryPop(out var node)) { yield return node; foreach (var child in flattener(node)) { toVisit.Push(child); } } } public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue) { return dictionary.TryGetValue(key, out var value) ? value : defaultValue; } }