diff --git a/Solutions/2024/Day18.cs b/Solutions/2024/Day18.cs index 745a189..653d905 100644 --- a/Solutions/2024/Day18.cs +++ b/Solutions/2024/Day18.cs @@ -34,6 +34,23 @@ public static int Part1(string[] _, params object[]? args) return shortestPath.Count - 1; // shortestPath includes start } + public static string Part2(string[] _, params object[]? args) + { + int gridSize = args.GridSize(); + int noOfBytes = args.Bytes(); + + Point start = Point.Zero; + Point end = new(gridSize - 1, gridSize - 1); + + List shortestPath = [Point.Zero]; + while (shortestPath is not []) { + shortestPath = FindShortestPath(start, end, _bytes.Take(++noOfBytes), gridSize); + } + + noOfBytes--; + return $"{_bytes[noOfBytes].X},{_bytes[noOfBytes].Y}"; + } + public static List FindShortestPath(Point start, Point goal, IEnumerable obstacles, int gridSize) { PriorityQueue priorityQueue = new(); @@ -89,31 +106,26 @@ private static List ReconstructPath(Dictionary cameFrom, Po return totalPath; } - - public static string Part2(string[] input, params object[]? args) => NO_SOLUTION_WRITTEN_MESSAGE; + private static int GridSize(this object[]? args) => GetArgument(args, 1, 71); + private static int Bytes(this object[]? args) => GetArgument(args, 2, 1024); private static void VisualiseRam(this IEnumerable bytes, string title, int gridSize, IEnumerable route, bool clearScreen = false) { + const char EMPTY = '.'; + const char BYTE = '#'; + const char PATH = 'O'; + if (_visualise is null) { return; } char[,] outputRamMap = new char[gridSize, gridSize]; - outputRamMap.FillInPlace('.'); + outputRamMap.FillInPlace(EMPTY); - foreach (Point position in bytes ?? []) { - outputRamMap[position.X, position.Y] = '#'; - } + foreach (Point p in bytes ?? []) { outputRamMap[p.X, p.Y] = BYTE; } + foreach (Point p in route ?? []) { outputRamMap[p.X, p.Y] = PATH; } - foreach (Point position in route ?? []) { - outputRamMap[position.X, position.Y] = 'O'; - } - - string[] output = ["", title, .. outputRamMap.AsStrings()/*.Select(s => s.Replace('.', ' '))*/]; + string[] output = ["", title, .. outputRamMap.AsStrings()]; _visualise?.Invoke(output, clearScreen); } - - private static int GridSize(this object[]? args) => GetArgument(args, 1, 71); - private static int Bytes(this object[]? args) => GetArgument(args, 2, 1024); - } diff --git a/Tests/2024/Tests_18.cs b/Tests/2024/Tests_18.cs index 59892ab..b1ec06c 100644 --- a/Tests/2024/Tests_18.cs +++ b/Tests/2024/Tests_18.cs @@ -39,6 +39,41 @@ public void Part1(string input, int gridSize, int bytes, int expected) } + [Theory] + [InlineData(""" + 5,4 + 4,2 + 4,5 + 3,0 + 2,1 + 6,3 + 2,4 + 1,5 + 0,6 + 3,3 + 2,6 + 5,1 + 1,2 + 5,5 + 2,5 + 6,5 + 1,4 + 0,4 + 6,4 + 1,1 + 6,1 + 1,0 + 0,5 + 1,6 + 2,0 + """, 7, 12, "6,1")] + public void Part2(string input, int gridSize, int bytes, string expected) + { + string actual = SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, new Action(Callback), gridSize, bytes); + actual.ShouldBe(expected); + } + + private void Callback(string[] lines, bool _) { if (lines is null or []) {