using System.Net.Http.Headers; 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)) .SelectMany(conn => conn[1] .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Select(to => (from: conn[0], to)) ); var connectionsGrid = GetConnectionsGrid(puzzleInput); return connectionsGrid; } private static Dictionary> GetConnectionsGrid(IEnumerable<(string, string)> puzzleInput) { var grid = new Dictionary>(); foreach (var (from, to) in puzzleInput) { AddOrUpdate(grid, from, [to]); AddOrUpdate(grid, to, [from]); } return grid; } private static void AddOrUpdate(Dictionary> dictionary, TKey key, HashSet value) where TKey : notnull { if(dictionary.TryGetValue(key, out var existing)) existing.UnionWith(value); else dictionary[key] = value; } public string SolvePart1(string pathToPuzzleInput) { var grid = ParsePuzzleInput(pathToPuzzleInput); var middle = GetMiddleNode(grid); return middle; } 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 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); 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); yield return (x1 + x2, y1 - y2); } } }