diff --git a/AoC_2026.Tests/Day06Test.cs b/AoC_2026.Tests/Day06Test.cs new file mode 100644 index 0000000..e6279e1 --- /dev/null +++ b/AoC_2026.Tests/Day06Test.cs @@ -0,0 +1,47 @@ +using AdvenOfCode.Contracts; + +namespace AoC_2026.Tests; + +public class Day06Test +{ + private readonly Day06 _sut; + private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day06.txt"; + private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day06.txt"; + + public Day06Test() + { + _sut = new Day06(); + } + [Fact] + public void Part01_Test_equals_6() + { + var actual = _sut.SolvePart1(TestInputPath); + + Assert.Equal("6", actual); + } + + [Fact] + public void Part01_Prod_equals_827() + { + var actual = _sut.SolvePart1(ProdInputPath); + + Assert.Equal("827", actual); + } + + [Fact] + public void Part02_Test_equals_35332647() + { + var actual = _sut.SolvePart2Test(TestInputPath); + + Assert.Equal("", actual); + } + + [Fact] + public void Part02_Prod_equals_40767683() + { + var actual = _sut.SolvePart2(ProdInputPath); + + Assert.Equal("", actual); + } +} \ No newline at end of file diff --git a/AoC_2026/Day01.cs b/AoC_2026/Day01.cs index 9a48b67..a8be6c7 100644 --- a/AoC_2026/Day01.cs +++ b/AoC_2026/Day01.cs @@ -4,7 +4,7 @@ namespace AoC_2026; public class Day01 : IPuzzleSolver { - private int[] ParsePuzzleInput(string path) + private static int[] ParsePuzzleInput(string path) { var puzzleInput = File.ReadAllLines(path) .Where(l => !string.IsNullOrWhiteSpace(l)) diff --git a/AoC_2026/Day06.cs b/AoC_2026/Day06.cs new file mode 100644 index 0000000..69380c9 --- /dev/null +++ b/AoC_2026/Day06.cs @@ -0,0 +1,158 @@ +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(); + + var missing = puzzleInput + .SelectMany(kv => kv.Value.Where(v => !puzzleInput.ContainsKey(v))) + .ToArray(); + foreach (var key in missing) + { + puzzleInput[key] = []; + } + + foreach (var (from, tos) in puzzleInput) + { + foreach (var to in tos) + { + puzzleInput[to].Add(from); + } + } + + return puzzleInput; + } + + 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)); + return middle.Key; + } + + public string SolvePart2(string pathToPuzzleInput) + { + var gridInstrucions = 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); + + return $"{location.x},{location.y}"; + } + + private static (int x, int y) DetermineLocation(string key, List<(string key, (int x, int y))> satellites, Dictionary> gridInstrucions) + { + var intersections = GetPossibleLocations(key, satellites, gridInstrucions); + + while (intersections.Count > 1) + { + var nextSat = GetNextPossibleSatellite(satellites, gridInstrucions); + satellites.Add(nextSat); + intersections = GetPossibleLocations(key, satellites, gridInstrucions); + } + + return intersections.First(); + } + + private static (string Key, (int x, int y) Pos) GetNextPossibleSatellite(List<(string key, (int x, int y))> satellites, Dictionary> gridInstrucions) + { + 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()); + } + + private static List<(int x, int y)> GetPossibleLocations(string key, List<(string key, (int x, int y))> satellites, Dictionary> gridInstrucions) + { + 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; + } + + 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) + { + var queue = new Queue<(string, int)>(); + queue.Enqueue((from, 0)); + var seen = new HashSet(); + while (queue.Count > 0) + { + var (current, distance) = queue.Dequeue(); + if (current == to) return distance; + if(!seen.Add(current)) continue; + foreach (var next in gridInstrucions[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 GetMaxDistanceFrom(string from, Dictionary> gridInstrucions) + { + var maxDistance = 0; + var queue = new Queue<(string, int)>(); + queue.Enqueue((from, 0)); + var seen = new HashSet(); + 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