static ProgramItem FindParent(ProgramItem myItem, List <ProgramItem> myProgramItems) { foreach (ProgramItem pi in myProgramItems) { if (myItem.parent == pi.name) { return(pi); } } return(myItem); }
static List <ProgramItem> FindImmediateChildren(ProgramItem myItem, List <ProgramItem> myProgramItems) { List <ProgramItem> childrenList = new List <ProgramItem>(); foreach (string childName in myItem.children) { foreach (ProgramItem pi in myProgramItems) { if (childName == pi.name) { childrenList.Add(pi); } } } return(childrenList); }
static void AssignTotalWeight(ProgramItem pi, List <ProgramItem> myProgramItems) { if (pi.totalWeight == 0) { List <ProgramItem> itsChildren = FindImmediateChildren(pi, myProgramItems); foreach (ProgramItem childpi in itsChildren) { if (childpi.totalWeight == 0) { AssignTotalWeight(childpi, myProgramItems); } pi.totalWeight += childpi.totalWeight; } pi.totalWeight += pi.weight; } }
static bool HasOutlier(ProgramItem pi, List <ProgramItem> myProgramItems) { int firstWeight = 0; foreach (ProgramItem child in FindImmediateChildren(pi, myProgramItems)) { if (firstWeight == 0) { firstWeight = child.totalWeight; } else if (firstWeight == child.totalWeight) { } else { return(true); } } return(false); }
static void Main(string[] args) { string input = @"C:\Users\jpowell\source\repos\AdventOfCode\AdventOfCode7-1\myInput.txt"; List <ProgramItem> myProgramItems = ReadProgramItems(input); AssignAllParents(myProgramItems); List <ProgramItem> absoluteChildren = FindAbsoluteChildren(myProgramItems); AssignTotalWeight(FindMasterProgramItem(myProgramItems), myProgramItems); ProgramItem currentProgramItem = FindMasterProgramItem(myProgramItems); while (HasOutlier(currentProgramItem, myProgramItems)) { currentProgramItem = FindOutlier(currentProgramItem, myProgramItems); } Console.WriteLine("PROBLEM PROGRAM ITEM:"); Console.WriteLine(currentProgramItem.ToString()); Console.WriteLine(); Console.WriteLine("ITS PARENT:"); Console.WriteLine(FindParent(currentProgramItem, myProgramItems).ToString()); Console.WriteLine(); Console.WriteLine("ITS CHILDREN:"); foreach (ProgramItem child in FindImmediateChildren(currentProgramItem, myProgramItems)) { Console.WriteLine(child.ToString()); } Console.WriteLine(); Console.WriteLine("ITS SIBLINGS:"); foreach (ProgramItem sibling in FindImmediateChildren(FindParent(currentProgramItem, myProgramItems), myProgramItems)) { Console.WriteLine(sibling.ToString()); } Console.ReadKey(); }
static ProgramItem FindOutlier(ProgramItem pi, List <ProgramItem> myProgramItems) { List <ProgramItem> immediateChildren = FindImmediateChildren(pi, myProgramItems); int indexOfOutlier = -1; while (indexOfOutlier == -1) { for (int i = 0; i < immediateChildren.Count - 2; i++) { int firstvalue = immediateChildren[i].totalWeight; int secondvalue = immediateChildren[i + 1].totalWeight; int thirdvalue = immediateChildren[i + 2].totalWeight; Console.WriteLine(firstvalue + "," + secondvalue + "," + thirdvalue); if (firstvalue == secondvalue && secondvalue == thirdvalue) { } else if (firstvalue == secondvalue) { indexOfOutlier = i + 2; break; } else if (firstvalue == thirdvalue) { indexOfOutlier = i + 1; break; } else if (secondvalue == thirdvalue) { indexOfOutlier = i; break; } } } return(immediateChildren[indexOfOutlier]); throw new Exception("No outliers exist!"); }