public static void ParseAndSetupProblem(string[] input, List <VillageProgram> village) { foreach (string list in input) { string[] parts = list.Split(' '); string programID = parts[0]; string[] connections = GetConnectionsFromInput(parts); VillageProgram program; if (!village.Exists(prog => prog.ID == programID)) { program = new VillageProgram(programID); } else { program = village.Find(prog => prog.ID == parts[0]); } foreach (string programId in connections) { VillageProgram con = new VillageProgram(programId); program.connections.Add(con); } village.Add(program); } }
static void Main() { string[] input = File.ReadAllLines("Day12\\Input\\input.txt"); List <VillageProgram> village = new List <VillageProgram>(); ParseAndSetupProblem(input, village); VillageProgram source = village.Find(program => program.ID == "0"); Console.WriteLine("Programs in group 0: " + BreadthFirstTraversal(source, village).Count); Console.ReadLine(); }
public static Dictionary <string, bool> BreadthFirstTraversal(VillageProgram source, List <VillageProgram> village) { Dictionary <string, bool> visited = new Dictionary <string, bool>(); Queue <VillageProgram> queue = new Queue <VillageProgram>(); visited.Add(source.ID, true); queue.Enqueue(source); while (queue.Count != 0) { VillageProgram program = queue.Dequeue(); program = village.Find(p => p.ID == program.ID); foreach (VillageProgram prog in program.connections) { if (!visited.ContainsKey(prog.ID)) { visited.Add(prog.ID, true); queue.Enqueue(prog); } } } return(visited); }