private OrbitObject GetOrAddObject(HashSet <OrbitObject> set, OrbitObject obj) { OrbitObject result = null; if (set.TryGetValue(obj, out result)) { return(result); } else { set.Add(obj); return(obj); } }
private HashSet <OrbitObject> LoadData() { var orbits = input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var orbitObjects = new HashSet <OrbitObject>(); foreach (string orbit in orbits) { var parent = new OrbitObject(orbit.Split(')')[0]); var satellite = new OrbitObject(orbit.Split(')')[1]); parent = GetOrAddObject(orbitObjects, parent); satellite = GetOrAddObject(orbitObjects, satellite); parent.Satellites.Add(satellite); satellite.Parent = parent; } return(orbitObjects); }