class Node { public char N; public List Precond = new List(); public List Postcond = new List(); } void Main() { var data = File .ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"07_input.txt")) .Where(p => !string.IsNullOrWhiteSpace(p)) .Select(l => (l[5], l[36])) .ToList(); var nodes = data.SelectMany(p => new[] {p.Item1, p.Item2}).Distinct().Select(p => new Node {N=p}).ToList(); foreach (var (pre, post) in data) { var n1 = nodes.First(p => p.N == pre); var n2 = nodes.First(p => p.N == post); n1.Postcond.Add(n2); n2.Precond.Add(n1); } var r = new StringBuilder(); for(var fin = new HashSet(); fin.Count !fin.Contains(p)).Where(p => p.Precond.All(q => fin.Contains(q))).OrderBy(p=>p.N).First(); fin.Add(n); r.Append(n.N); } r.ToString().Dump(); } // Define other methods and classes here