示例#1
0
        public string SolvePart1()
        {
            var orbitMap = new OrbitMap();

            orbitMap.Build(Input);

            return($"Part 1: {orbitMap.OrbitChecksum}");
        }
示例#2
0
        public string SolvePart2()
        {
            var orbitMap = new OrbitMap();

            orbitMap.Build(Input);

            if (orbitMap.CenterOfMass == null)
            {
                throw new Exception("Something went wrong building the Orbit Map");
            }

            // 1. Find what you're orbiting
            OrbitingObject?yourOrbit = FindOrbitingObject(orbitMap.CenterOfMass, "YOU");

            // 2. Find what Santa's orbiting
            OrbitingObject?santasOrbit = FindOrbitingObject(orbitMap.CenterOfMass, "SAN");

            var    visitedByYou     = new Dictionary <string, int>();
            var    visitedBySanta   = new Dictionary <string, int>();
            string commonObjectName = string.Empty;

            if (yourOrbit != null && santasOrbit != null)
            {
                // 3. Figure out how to get from whatever YOU are orbiting to whatever SAN is orbiting
                OrbitingObject?path1 = yourOrbit;
                visitedByYou.Add(path1.Name ?? "UNK", 0);
                var            yourJumpCount = 0;
                OrbitingObject?path2         = santasOrbit;
                visitedBySanta.Add(path2.Name ?? "UNK", 0);
                var santasJumpCount = 0;

                while (string.IsNullOrWhiteSpace(commonObjectName))
                {
                    if (path1 != null && string.IsNullOrWhiteSpace(commonObjectName))
                    {
                        path1 = FindOrbitingObject(orbitMap.CenterOfMass, path1.Name ?? "UNK");
                        if (path1 != null)
                        {
                            yourJumpCount++;
                            visitedByYou.Add(path1.Name ?? "UNK", yourJumpCount);

                            // Exit condition
                            commonObjectName = path1.Name != null && visitedBySanta.ContainsKey(path1.Name) ? path1.Name : string.Empty;
                        }
                    }

                    if (path2 != null && string.IsNullOrWhiteSpace(commonObjectName))
                    {
                        path2 = FindOrbitingObject(orbitMap.CenterOfMass, path2.Name ?? "UNK");
                        if (path2 != null)
                        {
                            santasJumpCount++;
                            visitedBySanta.Add(path2.Name ?? "UNK", santasJumpCount);

                            // Exit condition
                            commonObjectName = path2.Name != null && visitedByYou.ContainsKey(path2.Name) ? path2.Name : string.Empty;
                        }
                    }
                }
            }

            return(visitedByYou.ContainsKey(commonObjectName) && visitedBySanta.ContainsKey(commonObjectName)
                                ? $"Part 2: {visitedByYou[commonObjectName] + visitedBySanta[commonObjectName]}"
                                : "Part 2: Unknown");
        }