コード例 #1
0
 private void BuildTree(Orbiter o, ref int count)
 {
     count++;
     if (DirectlyOrbits.ContainsKey(o.Name))
     {
         o.InOrbit = DirectlyOrbits[o.Name];
         foreach (var orbiter in o.InOrbit)
         {
             BuildTree(orbiter, ref count);
         }
     }
 }
コード例 #2
0
 public override void VisitEnd(Orbiter o)
 {
     if (o.IsLeaf)
     {
         o.TotalOrbiters = 0;
     }
     else
     {
         o.TotalOrbiters = o.InOrbit.Sum((orbiter => orbiter.TotalOrbiters + 1));
     }
     CheckSum += o.TotalOrbiters;
 }
コード例 #3
0
        public void ParseInput(string filename)
        {
            var lines = File.ReadAllLines(filename);

            DirectlyOrbits = new Dictionary <string, List <Orbiter> >();
            foreach (var line in lines)
            {
                var names = line.Split(')');
                if (names.Length != 2)
                {
                    throw new Exception($"Wrong kind of input: {line}");
                }
                var orbiter = new Orbiter(names[0], names[1]);
                if (names[1] == "YOU")
                {
                    _you = orbiter;
                }
                if (names[1] == "SAN")
                {
                    _santa = orbiter;
                }

                if (DirectlyOrbits.ContainsKey(names[0]))
                {
                    DirectlyOrbits[names[0]].Add(orbiter);
                }
                else
                {
                    DirectlyOrbits.Add(names[0], new List <Orbiter>()
                    {
                        orbiter
                    });
                }
            }

            _com = new Orbiter(null, "COM");
            int numNodes = 0;

            BuildTree(_com, ref numNodes);

            Console.WriteLine($"lines {lines.Length} ? Number of nodes {numNodes} ");
        }
コード例 #4
0
        public override void VisitEnd(Orbiter o)
        {
            if (Done)
            {
                return;
            }
            if (o.IsLeaf)
            {
                if (o.Name == "SAN")
                {
                    IsBacktrackingSanta = true;
                    o.LeadsToSanta      = true;
                }
                else if (o.Name.Equals("YOU"))
                {
                    IsBacktrackingYou = true;
                    o.LeadsToYou      = true;
                }
            }
            else
            {
                IsBacktrackingSanta = o.InOrbit.Any(x => x.LeadsToSanta) || IsBacktrackingSanta;
                IsBacktrackingYou   = o.InOrbit.Any(x => x.LeadsToYou) || IsBacktrackingYou;

                if (IsBacktrackingYou && IsBacktrackingSanta)
                {
                    Done = true;
                }
                else if (IsBacktrackingSanta)
                {
                    o.LeadsToSanta = true;
                    OrbitalChanges++;
                }
                else if (IsBacktrackingYou)
                {
                    o.LeadsToYou = true;
                    OrbitalChanges++;
                }
            }
        }
コード例 #5
0
 public override void VisitStart(Orbiter o)
 {
 }
コード例 #6
0
 public override void VisitStart(Orbiter o)
 {
     IsBacktrackingSanta = false;
     IsBacktrackingYou   = false;
 }
コード例 #7
0
 abstract public void VisitEnd(Orbiter o);
コード例 #8
0
 abstract public void VisitStart(Orbiter o);