Beispiel #1
0
        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);
        }
Beispiel #2
0
 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;
     }
 }
Beispiel #3
0
        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);
        }