Guest User

2024day20straightForward

a guest
Dec 20th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public static class Day20
  2. {
  3. private static string[] Grid;
  4. private static int[] d = [0, 1, 2, 3];
  5. private static int[] dx = [1, 0, -1, 0];
  6. private static int[] dy = [0, 1, 0, -1];
  7. private static (int x, int y) Start, Ende;
  8.  
  9. public static int Solve1(string input) => Solve(input, 2);
  10.  
  11. public static int Solve2(string input) => Solve(input, 20);
  12.  
  13. public static int Solve(string input, int cheatSteps)
  14. {
  15. Grid = input.Split(Environment.NewLine);
  16. ScanGrid();
  17. (int x, int y)[] path = BFS(Start, Nexts, includeWhen, skipWhen, exitWhen).ToArray;
  18. List<int> result = new();
  19. for (int i = 0; i < path.Length - 3; i++)
  20. {
  21. for (int j = i + 3; j < path.Length; j++)
  22. {
  23. int diff = Math.Abs(path(j).x - path(i).x) + Math.Abs(path(j).y - path(i).y);
  24. if (diff <= cheatSteps)
  25. result.Add(j - i - diff);
  26. }
  27. }
  28. return result.AsEnumerable().Count(i => i > 99);
  29. }
  30. private static void ScanGrid()
  31. {
  32. for (int y = 0; y < Grid.Length; y++)
  33. {
  34. for (int x = 0; x < Grid[0].Length; x++)
  35. {
  36. if (Grid[y][x] == 'S')
  37. Start = (x, y);
  38. if (Grid[y][x] == 'E')
  39. Ende = (x, y);
  40. }
  41. }
  42. }
  43. private static (int x, int y)[] Nexts((int x, int y) p) => d.Select(i => (p.x + dx[i], p.y + dy[i])).Where(q => CheckGridBound(q, Grid, '#')).ToArray;
  44.  
  45. private static bool includeWhen((int x, int y) _step) => true;
  46.  
  47. private static bool skipWhen((int x, int y) _step) => false;
  48.  
  49. private static bool exitWhen((int x, int y) _step) => return _step.Equals(Ende);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment