Skip to content

Commit

Permalink
2024 Day18 Solved (a little slow)
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 18, 2024
1 parent 7855402 commit 552bbb4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
42 changes: 27 additions & 15 deletions Solutions/2024/Day18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point> 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<Point> FindShortestPath(Point start, Point goal, IEnumerable<Point> obstacles, int gridSize)
{
PriorityQueue<Point, int> priorityQueue = new();
Expand Down Expand Up @@ -89,31 +106,26 @@ private static List<Point> ReconstructPath(Dictionary<Point, Point> 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<Point> bytes, string title, int gridSize, IEnumerable<Point> 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);

}
35 changes: 35 additions & 0 deletions Tests/2024/Tests_18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[], bool>(Callback), gridSize, bytes);
actual.ShouldBe(expected);
}


private void Callback(string[] lines, bool _)
{
if (lines is null or []) {
Expand Down

0 comments on commit 552bbb4

Please sign in to comment.