public static Dictionary <string, RecursionProgram> CalculateRecursionTree(IEnumerable <string> input) { var allPrograms = new Dictionary <string, RecursionProgram>(); foreach (var serializedInput in input) { var strippedInput = serializedInput.Replace(" ", string.Empty).Replace(">", string.Empty); var thisAndAbove = strippedInput.Split('-'); GetNameAndWeight(thisAndAbove[0], out var name, out var weight); var program = new RecursionProgram { Name = name, Weight = weight, CombinedWeight = weight, ProgramsAbove = thisAndAbove.Length == 1 ? null : thisAndAbove[1]?.Split(',').Select(x => x) }; allPrograms.Add(program.Name, program); } foreach (var recursionProgram in allPrograms.Values) { var programBelow = allPrograms.Values.SingleOrDefault(x => x.ProgramsAbove?.SingleOrDefault(y => y.Equals(recursionProgram.Name)) != null); if (programBelow != null) { recursionProgram.ProgramBelow = programBelow; } } return(allPrograms); }
private static int CalculateCombinedWeight(RecursionProgram program) { foreach (var programName in program.ProgramsAbove) { } return(0); }
private static void SortTheTree(IDictionary <string, RecursionProgram> unsortedTree, IDictionary <int, List <RecursionProgram> > sortedTree) { var treeLevel = 1; RecursionProgram lastProgramOnLevel = null; do { var previousLevel = sortedTree[treeLevel - 1]; var thisLevel = new List <RecursionProgram>(); sortedTree.Add(treeLevel, thisLevel); foreach (var recursionProgram in previousLevel) { lastProgramOnLevel = recursionProgram.ProgramBelow; if (!sortedTree[treeLevel].Contains(lastProgramOnLevel)) { thisLevel.Add(recursionProgram.ProgramBelow); } lastProgramOnLevel.CombinedWeight += recursionProgram.CombinedWeight; } treeLevel++; } while (lastProgramOnLevel?.ProgramBelow != null); }
private static int CalculateCombinedWeights(IDictionary <string, RecursionProgram> allPrograms, RecursionProgram program, int treeLevel = 0) { foreach (var programAboveName in program.ProgramsAbove ?? new List <string>()) { var programAbove = allPrograms[programAboveName]; program.CombinedWeight += CalculateCombinedWeights(allPrograms, programAbove, treeLevel + 1); } return(program.CombinedWeight); }