public AstronomicalChart Build(string input)
        {
            input = input.Trim();
            var commands = Split(input).ToList();
            var objects  = new Dictionary <string, AstronomicalObject>();

            AstronomicalChart result = null;

            foreach (var command in commands)
            {
                AstronomicalObject primary;
                if (!objects.TryGetValue(command.Item1, out primary))
                {
                    primary = new AstronomicalObject(command.Item1);
                    objects.Add(primary.Name, primary);
                }

                if (result == null && primary.Name == "COM")
                {
                    result = new AstronomicalChart(primary);
                }

                AstronomicalObject satellite;
                if (!objects.TryGetValue(command.Item2, out satellite))
                {
                    satellite = new AstronomicalObject(command.Item2);
                    objects.Add(satellite.Name, satellite);
                }

                primary.AddSatellite(satellite);
            }

            return(result);
        }
        public void AddSatellite(AstronomicalObject satellite)
        {
            if (satellite == null)
            {
                throw new ArgumentNullException(nameof(satellite));
            }

            satellite.SetPrimary(this);
            this.satellites.Add(satellite);
        }
Exemple #3
0
        private IEnumerable <AstronomicalObject> GetParents(AstronomicalObject start)
        {
            var current = start;

            do
            {
                yield return(current);

                current = current.Primary;
            }while (current != null);
        }
Exemple #4
0
        private AstronomicalObject SearchGraph(AstronomicalObject start, string searchFor)
        {
            if (start.Name == searchFor)
            {
                return(start);
            }

            foreach (var satellite in start.Satellites)
            {
                var result = SearchGraph(satellite, searchFor);

                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
Exemple #5
0
        private AstronomicalObject FindCommonAncestor(IEnumerable <AstronomicalObject> pathToStart, IEnumerable <AstronomicalObject> pathToDestination)
        {
            var path1 = pathToStart.ToList();
            var path2 = pathToDestination.ToList();

            AstronomicalObject lastMatch = null;

            for (int i = 0; i < Math.Min(path1.Count, path2.Count); i++)
            {
                if (path1[i].Name == path2[i].Name)
                {
                    lastMatch = path1[i];
                }
                else
                {
                    break;
                }
            }

            return(lastMatch);
        }
 public void SetPrimary(AstronomicalObject primary)
 {
     Primary = primary ?? throw new ArgumentNullException(nameof(primary));
 }