public DiscNode CreateNewOrGetExistingDisc(List <DiscNode> towerOfDiscs, string name) { DiscNode disc = towerOfDiscs.SingleOrDefault(d => d.Name == name); if (disc == null) { disc = new DiscNode { Name = name, Children = new List <DiscNode>() }; towerOfDiscs.Add(disc); } return(disc); }
public void CreateBalanceOnDisc(string line, DiscNode disc, List <DiscNode> towerOfDiscs) { string[] childNames = GetChildrenNames(line); if (childNames == null) { return; } foreach (string childName in childNames) { DiscNode child = CreateNewOrGetExistingDisc(towerOfDiscs, childName); disc.Children.Add(child); child.Parent = disc; } }
public int Part2Solve(string[] lines) { List <DiscNode> towerOfDiscs = new List <DiscNode>(); foreach (string line in lines) { string name = GetDiscName(line); DiscNode disc = CreateNewOrGetExistingDisc(towerOfDiscs, name); CreateBalanceOnDisc(line, disc, towerOfDiscs); } DiscNode root = towerOfDiscs.Where(ti => ti.Parent == null).Single(); return(root.Weight); }