Advertisement
mgla

Advent of Code 2023 - Day 23

Dec 23rd, 2023
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. var input = File.ReadAllLines("input.txt");
  2.  
  3. var start = new Position(0, 1);
  4. var end = new Position(input.Length - 1, input[0].Length - 2);
  5.  
  6. var graph = BuildGraph(input);
  7. Console.WriteLine($"Part 1: {FindLongestPath(graph, [], start, end)}");
  8.  
  9. graph = BuildGraph(input, true);
  10. Console.WriteLine($"Part 2: {FindLongestPath(graph, [], start, end)}");
  11.  
  12. return;
  13.  
  14. static Dictionary<Position, HashSet<Position>> BuildGraph(string[] input, bool part2 = false)
  15. {
  16.     var graph = new Dictionary<Position, HashSet<Position>>();
  17.     for (var row = 0; row < input.Length; row++)
  18.     {
  19.         var line = input[row];
  20.  
  21.         for (var col = 0; col < line.Length; col++)
  22.         {
  23.             if (line[col] == '#')
  24.             {
  25.                 continue;
  26.             }
  27.  
  28.             var pos = new Position(row, col);
  29.             graph[pos] = [];
  30.  
  31.             if (!part2)
  32.             {
  33.                 switch (line[col])
  34.                 {
  35.                     case '>':
  36.                         graph[pos].Add(pos.Move(Direction.Right));
  37.                         continue;
  38.                     case 'v':
  39.                         graph[pos].Add(pos.Move(Direction.Down));
  40.                         continue;
  41.                     case '<':
  42.                         graph[pos].Add(pos.Move(Direction.Left));
  43.                         continue;
  44.                 }
  45.             }
  46.  
  47.             if (row > 0 && input[row - 1][col] != '#')
  48.             {
  49.                 graph[pos].Add(pos.Move(Direction.Up));
  50.             }
  51.  
  52.             if (row < input.Length - 1 && input[row + 1][col] != '#')
  53.             {
  54.                 graph[pos].Add(pos.Move(Direction.Down));
  55.             }
  56.  
  57.             if (col > 0 && input[row][col - 1] != '#')
  58.             {
  59.                 graph[pos].Add(pos.Move(Direction.Left));
  60.             }
  61.  
  62.             if (col < line.Length - 1 && input[row][col + 1] != '#')
  63.             {
  64.                 graph[pos].Add(pos.Move(Direction.Right));
  65.             }
  66.         }
  67.     }
  68.  
  69.     return graph;
  70. }
  71.  
  72.  
  73. static int FindLongestPath(Dictionary<Position, HashSet<Position>> graph, HashSet<Position> visited, Position current, Position end)
  74. {
  75.     var result = 0;
  76.  
  77.     if (current == end)
  78.     {
  79.         return visited.Count;
  80.     }
  81.  
  82.     foreach (var neighbor in graph[current])
  83.     {
  84.         if (!visited.Add(neighbor))
  85.         {
  86.             continue;
  87.         }
  88.  
  89.         var length = FindLongestPath(graph, visited, neighbor, end);
  90.         result = Math.Max(result, length);
  91.         visited.Remove(neighbor);
  92.     }
  93.  
  94.     return result;
  95. }
  96.  
  97.  
  98. internal record Direction(int Row, int Col)
  99. {
  100.     public static Direction Up = new(-1, 0);
  101.     public static Direction Down = new(1, 0);
  102.     public static Direction Left = new(0, -1);
  103.     public static Direction Right = new(0, 1);
  104. }
  105.  
  106. internal record Position(int Row, int Col)
  107. {
  108.     public Position Move(Direction dir)
  109.     {
  110.         return new Position(Row + dir.Row, Col + dir.Col);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement