Advertisement
Guest User

Advent of Code Day 12 solution

a guest
Dec 15th, 2022
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | Source Code | 0 0
  1. var input = File.ReadAllLines(@"..\..\..\Inputs\Day12.txt");
  2. int mapHeight = input.Length;
  3. int mapWidth = input[0].Trim().Length;
  4. var heightmap = new int[mapHeight, mapWidth];
  5. var start = ((0, 0), 0);
  6. var end = ((0, 0), 0);
  7.  
  8. for (int x = 0; x < mapHeight; x++)
  9. {
  10.     for (int y = 0; y < mapWidth; y++)
  11.     {
  12.         if (input[x][y] == 'S')
  13.         {
  14.             heightmap[x, y] = 1;
  15.             start = ((x, y), 1);
  16.         }
  17.         else if (input[x][y] == 'E')
  18.         {
  19.             heightmap[x, y] = 26;
  20.             end = ((x, y), 26);
  21.         }
  22.         else
  23.             heightmap[x, y] = input[x][y] - 'a' + 1;
  24.     }
  25. }
  26.  
  27. int fewestStepsFromStart = GetShortestPath(heightmap, start, end, true);
  28. int fewestStepsOverall = GetShortestPath(heightmap, start, end, false);
  29.  
  30. Console.WriteLine($"The fewest steps from the starting position: {fewestStepsFromStart}\nThe fewest steps from any lowest position: {fewestStepsOverall}.\n");
  31. Console.ReadKey();
  32.  
  33. //Breadth-first search algorithm - procedure BFS(graph G, starting vertex root of G)
  34. private static int GetShortestPath(int[,] heightmap, ((int, int), int) start, ((int, int), int) end, bool isPartOne)
  35. {
  36.     int mapHeight = heightmap.GetLength(0);
  37.     int mapWidth = heightmap.GetLength(1);
  38.     List<(int x, int y)> neighbours = new List<(int x, int y)>() { (-1, 0), (1, 0), (0, -1), (0, 1) };
  39.  
  40.     //let Q be a queue
  41.     var queue = new Queue<((int x, int y), int step)>();
  42.     //(a list to store the visited nodes)
  43.     var visited = new HashSet<(int x, int y)>();
  44.  
  45.     //label root as explored
  46.     //(start from S for part 1, and from any node of height value 1 for part 2)
  47.     if (isPartOne)
  48.         queue.Enqueue(((start.Item1.Item1, start.Item1.Item2), 0));
  49.     else
  50.     {
  51.         for (int x = 0; x < mapHeight; x++)
  52.         {
  53.             for (int y = 0; y < mapWidth; y++)
  54.                 if (heightmap[x, y] == 1)
  55.                 //Q.enqueue(root)
  56.                 queue.Enqueue(((x, y), 0));
  57.         }
  58.     }
  59.  
  60.     //while Q is not empty do
  61.     while (queue.Any())
  62.     {
  63.         //v := Q.dequeue() - define the current node as the first in the queue
  64.         ((int x, int y), int step) = queue.Dequeue();
  65.  
  66.         //(check if current node has been visited, if yes, skip exploration and adding to the queue)
  67.         if (!visited.Add((x, y)))
  68.             continue;
  69.  
  70.         //if v is the goal then - current node is end node, so break the loop
  71.         if (x == end.Item1.Item1 && y == end.Item1.Item2)
  72.             //return v
  73.             return step;
  74.  
  75.         //for all edges from v to w in G.adjacentEdges(v) do
  76.         foreach ((int dx, int dy) in neighbours)
  77.         {
  78.             var dxPos = x + dx;
  79.             var dyPos = y + dy;
  80.  
  81.             //if w is not labeled as explored then
  82.             //label w as explored
  83.             if ((dxPos >= 0 && dxPos < mapHeight) && (dyPos >= 0 && dyPos < mapWidth))
  84.             {
  85.                 //w.parent := v - define the child node and the current node as its parent
  86.                 var parentNode = heightmap[x, y];
  87.                 var childNode = heightmap[dxPos, dyPos];
  88.  
  89.                 //Q.enqueue(w)
  90.                 if (childNode - parentNode <= 1)
  91.                 queue.Enqueue(((dxPos, dyPos), step + 1));
  92.             }
  93.         }
  94.     }
  95.     //no roots (starting node(s)), so no path
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement