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); } } }
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; }
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} "); }
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++; } } }
public override void VisitStart(Orbiter o) { }
public override void VisitStart(Orbiter o) { IsBacktrackingSanta = false; IsBacktrackingYou = false; }
abstract public void VisitEnd(Orbiter o);
abstract public void VisitStart(Orbiter o);