using AdvenOfCode.Contracts; using AdventOfCode.Extensions; namespace AoC_2026; public class Day06 : IPuzzleSolver { private static Dictionary> ParsePuzzleInput(string path) { var puzzleInput = File.ReadAllLines(path) .Where(l => !string.IsNullOrWhiteSpace(l)) .Select(l => l.Split('-', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) .Select(conn => (conn[0], conn[1].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet())) .ToDictionary(); AddMissingKeys(puzzleInput); AddAllVertices(puzzleInput); return puzzleInput; } private static void AddAllVertices(Dictionary> puzzleInput) { foreach (var (from, tos) in puzzleInput) { foreach (var to in tos) { puzzleInput[to].Add(from); } } } private static void AddMissingKeys(Dictionary> puzzleInput) { var missing = puzzleInput .SelectMany(kv => kv.Value.Where(v => !puzzleInput.ContainsKey(v))) .ToArray(); foreach (var key in missing) { puzzleInput[key] = []; } } 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; } public string SolvePart2Test(string pathToPuzzleInput) { var gridInstructions = ParsePuzzleInput(pathToPuzzleInput); List<(string key, (int x, int y))> satellites = [("1", (11,20)), ("2", (10,22)), ("3", (12,20))]; var location = DetermineLocation("7", satellites, gridInstructions); return $"{location.x},{location.y}"; } public string SolvePart2(string pathToPuzzleInput) { var gridInstructions = ParsePuzzleInput(pathToPuzzleInput); List<(string key, (int x, int y))> satellites = [("1", (47,30)), ("2", (29,59)), ("3", (56,35))]; var location = DetermineLocation("2026", satellites, gridInstructions); return $"{location.x},{location.y}"; } 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); while (intersections.Length > 1) { var nextSat = GetNextPossibleSatellite(satellites, gridInstructions); satellites.Add(nextSat); intersections = GetPossibleLocations(key, satellites, gridInstructions); } return intersections.First(); } private static (string Key, (int x, int y) Pos) GetNextPossibleSatellite(List<(string key, (int x, int y) point)> satellites, Dictionary> gridInstructions) { var nextSat = gridInstructions .Where(satellite => satellites.All(sat => sat.key != satellite.Key)) .Select(satellite => (satellite.Key, locations: GetPossibleLocations(satellite.Key, satellites, gridInstructions))) .First(satellite => satellite.locations.Length == 1); return (nextSat.Key, nextSat.locations.First()); } private static (int x, int y)[] GetPossibleLocations(string key, List<(string key, (int x, int y) point)> satellites, Dictionary> gridInstructions) { return [ .. satellites .Select(satellite => (satellite, dist: GetMinDistanceTo(satellite.key, key, gridInstructions))) .Select(tuple => GetPointsOnRadius(tuple.satellite.point, tuple.dist)) .Aggregate((acc, next) => acc.Intersect(next)) ]; } private static int GetMinDistanceTo(string from, string to, Dictionary> gridInstructions) { var queue = new Queue<(string, int)>(); var seen = new HashSet(); queue.Enqueue((from, 0)); while (queue.Count > 0) { var (current, distance) = queue.Dequeue(); if (current == to) return distance; if(!seen.Add(current)) continue; foreach (var next in gridInstructions[current]) { queue.Enqueue((next, distance + 1)); } } return -1; } private static IEnumerable<(int x, int y)> GetPointsOnRadius((int x, int y) center, int radius) { var (x1, y1) = center; for(var x2 = -radius; x2 <= radius; x2++) { var y2 = Math.Abs(Math.Abs(x2) - radius); yield return (x1 + x2, y1 + y2); if(y2 != 0) 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; } }