Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Day20
- {
- private static string[] Grid;
- private static int[] d = [0, 1, 2, 3];
- private static int[] dx = [1, 0, -1, 0];
- private static int[] dy = [0, 1, 0, -1];
- private static (int x, int y) Start, Ende;
- public static int Solve1(string input) => Solve(input, 2);
- public static int Solve2(string input) => Solve(input, 20);
- public static int Solve(string input, int cheatSteps)
- {
- Grid = input.Split(Environment.NewLine);
- ScanGrid();
- (int x, int y)[] path = BFS(Start, Nexts, includeWhen, skipWhen, exitWhen).ToArray;
- List<int> result = new();
- for (int i = 0; i < path.Length - 3; i++)
- {
- for (int j = i + 3; j < path.Length; j++)
- {
- int diff = Math.Abs(path(j).x - path(i).x) + Math.Abs(path(j).y - path(i).y);
- if (diff <= cheatSteps)
- result.Add(j - i - diff);
- }
- }
- return result.AsEnumerable().Count(i => i > 99);
- }
- private static void ScanGrid()
- {
- for (int y = 0; y < Grid.Length; y++)
- {
- for (int x = 0; x < Grid[0].Length; x++)
- {
- if (Grid[y][x] == 'S')
- Start = (x, y);
- if (Grid[y][x] == 'E')
- Ende = (x, y);
- }
- }
- }
- 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;
- private static bool includeWhen((int x, int y) _step) => true;
- private static bool skipWhen((int x, int y) _step) => false;
- private static bool exitWhen((int x, int y) _step) => return _step.Equals(Ende);
- }
Advertisement
Add Comment
Please, Sign In to add comment