Advertisement
mgla

Advent of Code 2023 - Day 17

Dec 17th, 2023 (edited)
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. var map = File.ReadAllLines("input.txt").Select(s => s.Select(c => int.Parse(c.ToString())).ToArray()).ToArray();
  2.  
  3. var queue = new PriorityQueue<Path, int>();
  4. var visited = new HashSet<string>();
  5.  
  6. queue.Enqueue(new Path(new(0, 0), Direction.Right, 0), 0);
  7.  
  8. var totalHeat = 0;
  9.  
  10. while (queue.Count > 0)
  11. {
  12.     var path = queue.Dequeue();
  13.    
  14.     if (path.Position.Row == map.Length - 1 && path.Position.Col == map[0].Length - 1)
  15.     {
  16.         totalHeat = path.Heat;
  17.         break;
  18.     }
  19.  
  20.     if (path.StraightLineLength < 3)
  21.     {
  22.         TryMove(path, path.Direction);
  23.     }
  24.  
  25.     TryMove(path, path.Direction.TurnLeft());
  26.     TryMove(path, path.Direction.TurnRight());
  27. }
  28.  
  29. Console.WriteLine($"Part 1: {totalHeat}");
  30.  
  31. queue.Clear();
  32. queue.Enqueue(new Path(new(0, 0), Direction.Right, 0), 0);
  33. queue.Enqueue(new Path(new(0, 0), Direction.Down, 0), 0);
  34. totalHeat = 0;
  35. visited.Clear();
  36.  
  37. while (queue.Count > 0)
  38. {
  39.     var path = queue.Dequeue();
  40.     if (path.Position.Row == map.Length - 1 && path.Position.Col == map[0].Length - 1 && path.StraightLineLength >= 4)
  41.     {
  42.         totalHeat = path.Heat;
  43.         break;
  44.     }
  45.  
  46.     if (path.StraightLineLength < 10)
  47.     {
  48.         TryMove(path, path.Direction);
  49.     }
  50.  
  51.     if (path.StraightLineLength >= 4)
  52.     {
  53.         TryMove(path, path.Direction.TurnLeft());
  54.         TryMove(path, path.Direction.TurnRight());
  55.     }
  56. }
  57.  
  58. Console.WriteLine($"Part 2: {totalHeat}");
  59. return;
  60.  
  61. void TryMove(Path path, Direction direction)
  62. {
  63.     var candidate = new Path(path.Position.Move(direction), direction, direction == path.Direction ? path.StraightLineLength + 1 : 1);
  64.  
  65.     if (candidate.Position.Row < 0 || candidate.Position.Row >= map.Length ||
  66.         candidate.Position.Col < 0 || candidate.Position.Col >= map[0].Length)
  67.     {
  68.         return;
  69.     }
  70.  
  71.     var key = $"{candidate.Position.Row},{candidate.Position.Col},{candidate.Direction.Row},{candidate.Direction.Col},{candidate.StraightLineLength}";
  72.     if (visited.Contains(key))
  73.     {
  74.         return;
  75.     }
  76.  
  77.     visited.Add(key);
  78.  
  79.     candidate.Heat = path.Heat + map[candidate.Position.Row][candidate.Position.Col];
  80.     queue.Enqueue(candidate, candidate.Heat);
  81. }
  82.  
  83. internal class Path(Position position, Direction direction, int straightLineLength)
  84. {
  85.     public readonly Position Position = position;
  86.     public readonly Direction Direction = direction;
  87.     public readonly int StraightLineLength = straightLineLength;
  88.     public int Heat { get; set; }
  89. }
  90.  
  91. internal class Direction(int row, int col)
  92. {
  93.     public readonly int Row = row;
  94.     public readonly int Col = col;
  95.  
  96.     public Direction TurnLeft()
  97.     {
  98.         return new Direction(-Col, Row);
  99.     }
  100.  
  101.     public Direction TurnRight()
  102.     {
  103.         return new Direction(Col, -Row);
  104.     }
  105.  
  106.     public static Direction Up = new(-1, 0);
  107.     public static Direction Down = new(1, 0);
  108.     public static Direction Left = new(0, -1);
  109.     public static Direction Right = new(0, 1);
  110. }
  111.  
  112. internal class Position(int row, int col)
  113. {
  114.     public readonly int Row = row;
  115.     public readonly int Col = col;
  116.    
  117.     public Position Move(Direction dir)
  118.     {
  119.         return new Position(Row + dir.Row, Col + dir.Col);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement