Advertisement
mgla

Advent of Code - 2024 - Day 16

Dec 16th, 2024 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. var timer = System.Diagnostics.Stopwatch.StartNew();
  2. var input = await File.ReadAllLinesAsync("input.txt");
  3.  
  4. var start = new Position(-1, -1);
  5. var end = new Position(-1, -1);
  6.  
  7. var gridSize = input.Length;
  8. var grid = new bool[gridSize, gridSize];
  9.  
  10. for (var row = 0; row < gridSize; row++)
  11. {
  12.     for (var col = 0; col < gridSize; col++)
  13.     {
  14.         grid[row, col] = true;
  15.  
  16.         switch (input[row][col])
  17.         {
  18.             case 'S':
  19.                 start = new Position(row, col);
  20.                 break;
  21.             case 'E':
  22.                 end = new Position(row, col);
  23.                 break;
  24.             case '#':
  25.                 grid[row, col] = false;
  26.                 break;
  27.         }
  28.     }
  29. }
  30.  
  31. var seen = new Dictionary<(Position Position, Direction direction), int>();
  32. var queue = new PriorityQueue<(Position Position, Direction Direction, HashSet<Position> Moves), int>();
  33. queue.Enqueue((start, Direction.Right, []), 0);
  34.  
  35. var shortestPaths = new HashSet<Position> { start };
  36. var shortestPathLength = 0;
  37.  
  38. while (queue.TryDequeue(out var current, out var cost))
  39. {
  40.     var (position, direction, moves) = current;
  41.  
  42.     if (position == end)
  43.     {
  44.         if (shortestPathLength == 0)
  45.         {
  46.             Console.WriteLine($"Part 1: {cost}");
  47.             shortestPathLength = cost;
  48.         }
  49.  
  50.         if (cost > shortestPathLength)
  51.         {
  52.             break;
  53.         }
  54.  
  55.         shortestPaths.UnionWith(moves);
  56.     }
  57.  
  58.     if (seen.GetValueOrDefault((position, direction), int.MaxValue) < cost)
  59.     {
  60.         continue;
  61.     }
  62.  
  63.     seen[(position, direction)] = cost;
  64.  
  65.     var next = position.Move(direction);
  66.     if (grid[next.Row, next.Col])
  67.     {
  68.         var nextMoves = new HashSet<Position>(moves) { next };
  69.         queue.Enqueue((next, direction, nextMoves), cost + 1);
  70.     }
  71.  
  72.     var left = direction.TurnLeft();
  73.     next = position.Move(left);
  74.     if (grid[next.Row, next.Col])
  75.     {
  76.         var nextMoves = new HashSet<Position>(moves) { next };
  77.         queue.Enqueue((next, left, nextMoves), cost + 1001);
  78.     }
  79.  
  80.     var right = direction.TurnRight();
  81.     next = position.Move(right);
  82.     if (grid[next.Row, next.Col])
  83.     {
  84.         var nextMoves = new HashSet<Position>(moves) { next };
  85.         queue.Enqueue((next, right, nextMoves), cost + 1001);
  86.     }
  87. }
  88.  
  89. Console.WriteLine($"Part 2: {shortestPaths.Count}");
  90. Console.WriteLine($"{timer.ElapsedMilliseconds}ms");
  91.  
  92. internal record Direction(int Row, int Col)
  93. {
  94.     public Direction TurnLeft() => new(-Col, Row);
  95.  
  96.     public Direction TurnRight() => new(Col, -Row);
  97.  
  98.     public static readonly Direction Up = new(-1, 0);
  99.     public static readonly Direction Down = new(1, 0);
  100.     public static readonly Direction Left = new(0, -1);
  101.     public static readonly Direction Right = new(0, 1);
  102. }
  103.  
  104. internal record Position(int Row, int Col)
  105. {
  106.     public Position Move(Direction dir) => new(Row + dir.Row, Col + dir.Col);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement