diff --git a/AoC_2026/Day06.cs b/AoC_2026/Day06.cs index 8e896ff..caf02ed 100644 --- a/AoC_2026/Day06.cs +++ b/AoC_2026/Day06.cs @@ -43,12 +43,11 @@ public class Day06 : IPuzzleSolver public string SolvePart1(string pathToPuzzleInput) { - var gridInstructions = ParsePuzzleInput(pathToPuzzleInput); - var possibleMiddles = gridInstructions.Where(conn => conn.Value.Count == 4); - var middle = possibleMiddles.MinBy(m => GetMaxDistanceFromEdge(m.Key, gridInstructions)); - return middle.Key; + var grid = ParsePuzzleInput(pathToPuzzleInput); + var middle = GetMiddleNode(grid); + return middle; } - + public string SolvePart2Test(string pathToPuzzleInput) { var gridInstructions = ParsePuzzleInput(pathToPuzzleInput); @@ -67,6 +66,34 @@ public class Day06 : IPuzzleSolver return $"{location.x},{location.y}"; } + private static string GetMiddleNode(Dictionary> grid) + { + while (grid.Keys.Count > 1) + { + var edges = GetEdges(grid); + foreach (var edge in edges) + { + grid.Remove(edge); + } + foreach (var (_, connections) in grid) + { + connections.ExceptWith(edges); + } + } + + return grid.First().Key; + } + + private static string[] GetEdges(Dictionary> grid) + { + var edges = grid + .Where(conn => conn.Value.Count < 4) + .Select(conn => conn.Key) + .ToArray(); + return edges; + } + + private static (int x, int y) DetermineLocation(string key, List<(string key, (int x, int y))> satellites, Dictionary> gridInstructions) { var intersections = GetPossibleLocations(key, satellites, gridInstructions); @@ -130,24 +157,4 @@ public class Day06 : IPuzzleSolver yield return (x1 + x2, y1 - y2); } } - - private static int GetMaxDistanceFromEdge(string from, Dictionary> gridInstrucions) - { - var maxDistance = 0; - var queue = new Queue<(string, int)>(); - var seen = new HashSet(); - queue.Enqueue((from, 0)); - while (queue.Count > 0) - { - var (current, distance) = queue.Dequeue(); - if(!seen.Add(current)) - continue; - maxDistance = Math.Max(maxDistance, distance); - foreach (var next in gridInstrucions[current]) - { - queue.Enqueue((next, distance + 1)); - } - } - return maxDistance; - } } \ No newline at end of file