feat: solved day 16 part 2

This commit is contained in:
Stevan Freeborn
2024-12-17 22:58:51 -06:00
parent 5b7dbd5b1d
commit 655b1ffd8f
8 changed files with 209 additions and 88 deletions
+4 -85
View File
@@ -1,5 +1,7 @@
using System.Diagnostics;
using ReindeerMaze;
if (args.Length is 0)
{
Console.WriteLine("Please provide a path to the input file.");
@@ -18,90 +20,7 @@ var input = await File.ReadAllLinesAsync(args[0]);
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = PuzzleSolver.Solve(input);
var result = PuzzleSolver.Solve(input, isPart2);
stopwatch.Stop();
Console.WriteLine($"The lowest score a Reindeer could get is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
static class PuzzleSolver
{
public static int Solve(string[] input)
{
List<(int Xd, int Yd)> directions = [
(1, 0),
(0, 1),
(-1, 0),
(0, -1),
];
var rows = input.Length;
var columns = input[0].Length;
(int X, int Y) start = (-1, -1);
(int X, int Y) end = (-1, -1);
for (var row = 0; row < rows; row++)
{
for (var column = 0; column < columns; column++)
{
var currentValue = input[row][column];
switch (currentValue)
{
case 'S':
start = (column, row);
break;
case 'E':
end = (column, row);
break;
}
}
}
var queue = new PriorityQueue<(int X, int Y, int Direction, int Score), int>();
var visited = new HashSet<(int X, int Y, int Direction)>();
var initialScore = 0;
var initialDirection = 0;
queue.Enqueue((start.X, start.Y, initialDirection, initialScore), initialScore);
while (queue.Count is not 0)
{
var current = queue.Dequeue();
var possibleVisited = (current.X, current.Y, current.Direction);
if (visited.Contains((possibleVisited)))
{
continue;
}
visited.Add(possibleVisited);
if (current.X == end.X && current.Y == end.Y)
{
return current.Score;
}
var currentDirection = directions[current.Direction];
var nextX = current.X + currentDirection.Xd;
var nextY = current.Y + currentDirection.Yd;
if (nextX >= 0 && nextX < columns && nextY >= 0 && nextY < rows && input[nextY][nextX] != '#')
{
var newScoreAfterMovingForward = current.Score + 1;
queue.Enqueue((nextX, nextY, current.Direction, newScoreAfterMovingForward), newScoreAfterMovingForward);
}
var newScoreAfterTurning = current.Score + 1000;
var directionAfterClockWiseTurn = (current.Direction + 1) % directions.Count;
queue.Enqueue((current.X, current.Y, directionAfterClockWiseTurn, newScoreAfterTurning), newScoreAfterTurning);
var directionAfterCounterClockWiseTurn = (current.Direction + 3) % directions.Count;
queue.Enqueue((current.X, current.Y, directionAfterCounterClockWiseTurn, newScoreAfterTurning), newScoreAfterTurning);
}
return 0;
}
}
Console.WriteLine($"The lowest score a Reindeer could get is {result}. ({stopwatch.ElapsedMilliseconds}ms)");