public void Insert(SolarBody body) { Bodies.Add(body); foreach (SolarBody b in Bodies.Where(w => w.Name == body.Parent)) { b.Children.Add(body); } }
public int CountYouToSanta() { SolarBody you = Bodies.Single(s => s.Name == "YOU"); SolarBody santa = Bodies.Single(s => s.Name == "SAN"); List <string> youToRoot = new List <string>(); List <string> santaToRoot = new List <string>(); SolarBody parent = Bodies.Single(s => s.Name == you.Parent); while (parent.Parent != null) { youToRoot.Add(parent.Name); parent = Bodies.Single(s => s.Name == parent.Parent); } parent = Bodies.Single(s => s.Name == santa.Parent); while (parent.Parent != null) { santaToRoot.Add(parent.Name); parent = Bodies.Single(s => s.Name == parent.Parent); } // Find first intersection string cross = youToRoot.Intersect(santaToRoot).FirstOrDefault(); Console.WriteLine("Crossing body: {0}", cross); List <string> youToCross = new List <string>(); List <string> santaToCross = new List <string>(); parent = Bodies.Single(s => s.Name == you.Name); while (parent.Parent != cross) { youToCross.Add(parent.Name); parent = Bodies.Single(s => s.Name == parent.Parent); } parent = Bodies.Single(s => s.Name == santa.Name); while (parent.Parent != cross) { santaToCross.Add(parent.Name); parent = Bodies.Single(s => s.Name == parent.Parent); } return(youToCross.Count() + santaToCross.Count()); }
public void Insert(OrbitPair op) { if (Bodies.Where(c => c.Name == op.Child).Count() == 1 && Bodies.Where(c => c.Name == op.Parent).Count() == 1) { SolarBody child = Bodies.Single(s => s.Name == op.Child); SolarBody parent = Bodies.Single(s => s.Name == op.Parent); child.Parent = op.Parent; parent.Children.Add(child); } else if (Bodies.Where(c => c.Name == op.Child).Count() == 1) { SolarBody child = Bodies.Single(s => s.Name == op.Child); SolarBody parent = new SolarBody(op.Parent); child.Parent = op.Parent; parent.Children.Add(child); Bodies.Add(parent); } else if (Bodies.Where(c => c.Name == op.Parent).Count() == 1) { SolarBody parent = Bodies.Single(s => s.Name == op.Parent); SolarBody child = new SolarBody(op.Child, op.Parent); parent.Children.Add(child); Bodies.Add(child); } else { SolarBody parent = new SolarBody(op.Parent); SolarBody child = new SolarBody(op.Child, op.Parent); parent.Children.Add(child); Bodies.Add(child); Bodies.Add(parent); } }