public CB AddChild(CB child) { DirectOrbit.Add(child); child.Parent = this; Inc(1); return(child); }
static void CreateMap(CB CenterOfMass, string[] input) { foreach (string s in input) { string[] val = s.Split(')'); if (val[0] == CenterOfMass.Name) { CreateMap(CenterOfMass.AddChild(new CB(val[1])), input); } } }
static void Main(string[] args) { string pathToFile = @"C:\Users\Eclipse\Documents\GitHub\AdventOfCode2019\input\day6.txt"; string[] data = ReadFile(pathToFile, '\r'); CB COM = new CB("COM"); CreateMap(COM, data); Console.WriteLine("COM Orbits : " + COM.Orbits); CB YOU = COM.FindCB("YOU"); CB SAN = COM.FindCB("SAN"); Console.WriteLine("Minimal Transfers : " + MinimalTransfers(YOU, SAN)); Console.ReadKey(); }
/// <summary> /// Searches for specified CB in all child orbitsé /// </summary> /// <param name="searchCB">Name of CB that is searched for.</param> public CB FindCB(string searchCB) { if (this.Name == searchCB) { return(this); } else { foreach (CB orbit in DirectOrbit) { CB found = orbit.FindCB(searchCB); if (found != null) { return(found); } } return(null); } }
static int MinimalTransfers(CB Source, CB Destination) { List <CB> SourceParents = Source.Parents(); List <CB> DestinationParents = Destination.Parents(); int counter1 = 0; int counter2; foreach (CB stellar1 in SourceParents) { counter2 = 0; foreach (CB stellar2 in DestinationParents) { if (stellar1 == stellar2) { return(counter1 + counter2 - 2); } counter2++; } counter1++; } return(-1); }