Exemplo n.º 1
0
 public void SetParent(SpaceObject parent)
 {
     Parent = parent;
 }
Exemplo n.º 2
0
        public void Run()
        {
            var lookup = new Dictionary <string, List <string> >();

            foreach ((var _1, var _2) in LoadData("Day6.txt"))
            {
                if (!lookup.TryGetValue(_1, out var list))
                {
                    list = new List <string>();
                    lookup.Add(_1, list);
                }

                list.Add(_2);
            }

            var currentSo = new SpaceObject {
                Id = "COM"
            };
            var spaceObjects = new Dictionary <string, SpaceObject>();

            spaceObjects.Add(currentSo.Id, currentSo);
            var q = new List <string>(new[] { currentSo.Id });

            while (q.Any())
            {
                currentSo = spaceObjects[q.First()];
                q.RemoveAt(0);
                var id = currentSo.Id;
                lookup.TryGetValue(id, out var lookupResult);
                foreach (var orbit in lookupResult ?? Enumerable.Empty <string>())
                {
                    if (!spaceObjects.TryGetValue(orbit, out var newSo))
                    {
                        newSo = new SpaceObject {
                            Id = orbit, Orbits = currentSo
                        };
                        spaceObjects.Add(orbit, newSo);
                    }

                    currentSo.OrbittedBy.Add(newSo);
                    q.Add(orbit);
                }
            }

            var sum = 0;

            foreach (var so in spaceObjects)
            {
                var o      = so.Value;
                var length = 0;
                while (o.Id != "COM")
                {
                    o = o.Orbits;
                    length++;
                }

                WriteLine($"{so.Key}: {length}");
                sum += length;
            }

            WriteLine($"Sum: {sum}");
        }