private void LinkOrbitData() { // first, set the 'orbits' field for each entry foreach (Moon moon in _moons) { Moon orbits = _moons.FirstOrDefault(m => m.Name == moon.OrbitsName); if (orbits != null) { moon.Orbits = orbits; } } // COM won't have been created - do it now; Moon com = _moons.FirstOrDefault(m => m.Name == "COM"); if (com == null) { _moons.Add(new Moon("COM", string.Empty)); // doesn't orbit anything } //now spin through, linking up the orbitedBy list foreach (Moon moon in _moons) { var tempMoons = _moons.Where(m => m.OrbitsName == moon.Name); if (tempMoons != null && moon.Orbits != null) { foreach (Moon tempMoon in tempMoons) { moon.AddOrbitedBy(tempMoon); } } } }
public int FindShortRoute() { List <string> youRoute = new List <string>(); List <string> sanRoute = new List <string>(); Moon you = _moons.FirstOrDefault(m => m.Name == "YOU"); Moon san = _moons.FirstOrDefault(m => m.Name == "SAN"); int steps = 0; string common = null; Moon curr = you.Orbits; while (curr.Orbits != null) { youRoute.Add(curr.OrbitsName); curr = curr.Orbits; } curr = san.Orbits; while (curr.Orbits != null) { if (youRoute.FirstOrDefault(m => m == curr.OrbitsName) != null) { steps++; common = curr.OrbitsName; break; } else { curr = curr.Orbits; steps++; } } // backtrack curr = you.Orbits; while (curr.Name != common) { steps++; curr = curr.Orbits; } return(steps); }
public void Puzzle1() { int totalOrbits = 0; var terminals = _moons.Where(m => m.OrbitedByCount == 0); foreach (Moon moon in _moons) { // travserse back to orgin, counting steps; int steps = string.IsNullOrEmpty(moon.OrbitsName) ? 0 : 1; Moon tempMoon = moon; while (tempMoon.Orbits != null) { tempMoon = tempMoon.Orbits; steps++; } totalOrbits += steps; } Console.WriteLine("Puzzle1 Total orbits = {0}", totalOrbits); }
public void AddOrbitedBy(Moon orbitedBy) { _orbitedBy.Add(orbitedBy); }