diff --git a/AoC_2026.Tests/Day06Test.cs b/AoC_2026.Tests/Day06Test.cs index e6279e1..d5ac389 100644 --- a/AoC_2026.Tests/Day06Test.cs +++ b/AoC_2026.Tests/Day06Test.cs @@ -30,18 +30,18 @@ public class Day06Test } [Fact] - public void Part02_Test_equals_35332647() + public void Part02_Test_equals_1222() { var actual = _sut.SolvePart2Test(TestInputPath); - Assert.Equal("", actual); + Assert.Equal("12,22", actual); } [Fact] - public void Part02_Prod_equals_40767683() + public void Part02_Prod_equals_4336() { var actual = _sut.SolvePart2(ProdInputPath); - Assert.Equal("", actual); + Assert.Equal("43,36", actual); } } \ No newline at end of file diff --git a/AoC_2026/Day06.cs b/AoC_2026/Day06.cs index 69380c9..949f58f 100644 --- a/AoC_2026/Day06.cs +++ b/AoC_2026/Day06.cs @@ -14,14 +14,14 @@ public class Day06 : IPuzzleSolver conn[1].Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet())) .ToDictionary(); - var missing = puzzleInput - .SelectMany(kv => kv.Value.Where(v => !puzzleInput.ContainsKey(v))) - .ToArray(); - foreach (var key in missing) - { - puzzleInput[key] = []; - } + AddMissingKeys(puzzleInput); + AddAllVertices(puzzleInput); + return puzzleInput; + } + + private static void AddAllVertices(Dictionary> puzzleInput) + { foreach (var (from, tos) in puzzleInput) { foreach (var to in tos) @@ -29,84 +29,81 @@ public class Day06 : IPuzzleSolver puzzleInput[to].Add(from); } } + } - return puzzleInput; + 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 gridInstrucions = ParsePuzzleInput(pathToPuzzleInput); - var possibleMiddles = gridInstrucions.Where(conn => conn.Value.Count == 4); - var middle = possibleMiddles.MinBy(m => GetMaxDistanceFrom(m.Key, gridInstrucions)); + 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 gridInstrucions = ParsePuzzleInput(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, gridInstrucions); + 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> gridInstrucions) + private static (int x, int y) DetermineLocation(string key, List<(string key, (int x, int y))> satellites, Dictionary> gridInstructions) { - var intersections = GetPossibleLocations(key, satellites, gridInstrucions); + var intersections = GetPossibleLocations(key, satellites, gridInstructions); - while (intersections.Count > 1) + while (intersections.Length > 1) { - var nextSat = GetNextPossibleSatellite(satellites, gridInstrucions); + var nextSat = GetNextPossibleSatellite(satellites, gridInstructions); satellites.Add(nextSat); - intersections = GetPossibleLocations(key, satellites, gridInstrucions); + 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))> satellites, Dictionary> gridInstrucions) + private static (string Key, (int x, int y) Pos) GetNextPossibleSatellite(List<(string key, (int x, int y) point)> satellites, Dictionary> gridInstructions) { - var nextSat = gridInstrucions - .Where(x => satellites.All(sat => sat.key != x.Key)) - .Select(x => (x.Key, GetPossibleLocations(x.Key, satellites, gridInstrucions))) - .First(x => x.Item2.Count == 1); - return (nextSat.Key, nextSat.Item2.First()); + 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 List<(int x, int y)> GetPossibleLocations(string key, List<(string key, (int x, int y))> satellites, Dictionary> gridInstrucions) + private static (int x, int y)[] GetPossibleLocations(string key, List<(string key, (int x, int y) point)> satellites, Dictionary> gridInstructions) { - List<(int x, int y)> intersections = []; - foreach (var (name, point) in satellites) - { - var distFromPoint = GetMinDistanceTo(name, key, gridInstrucions); - var pointsFromPoint = GetPointsOnRadius(point, distFromPoint); - if(!intersections.Any()) - intersections = [.. pointsFromPoint]; - else - intersections = [..intersections.Intersect(pointsFromPoint)]; - } - - return intersections; + 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)) + ]; } - public string SolvePart2Test(string pathToPuzzleInput) - { - var gridInstrucions = ParsePuzzleInput(pathToPuzzleInput); - var distFromOne = GetMinDistanceTo("1", "7", gridInstrucions); - var distFromTwo = GetMinDistanceTo("2", "7", gridInstrucions); - var distFromThree = GetMinDistanceTo("3", "7", gridInstrucions); - - var pointsFromOne = GetPointsOnRadius((11,20), distFromOne); - var pointsFromTwo = GetPointsOnRadius((10,22), distFromTwo); - var pointsFromThree = GetPointsOnRadius((12,20), distFromThree); - - var intersections = pointsFromOne.Intersect(pointsFromTwo).Intersect(pointsFromThree); - var intersection = intersections.First(); - - return $"{intersection.x},{intersection.y}"; - } - private static int GetMinDistanceTo(string from, string to, Dictionary> gridInstrucions) + private static int GetMinDistanceTo(string from, string to, Dictionary> gridInstructions) { var queue = new Queue<(string, int)>(); queue.Enqueue((from, 0)); @@ -116,7 +113,7 @@ public class Day06 : IPuzzleSolver var (current, distance) = queue.Dequeue(); if (current == to) return distance; if(!seen.Add(current)) continue; - foreach (var next in gridInstrucions[current]) + foreach (var next in gridInstructions[current]) { queue.Enqueue((next, distance + 1)); } @@ -136,7 +133,7 @@ public class Day06 : IPuzzleSolver } } - private static int GetMaxDistanceFrom(string from, Dictionary> gridInstrucions) + private static int GetMaxDistanceFromEdge(string from, Dictionary> gridInstrucions) { var maxDistance = 0; var queue = new Queue<(string, int)>();