From 9e0c8028e076ffa16aa3140cfc727de8ec2fa632 Mon Sep 17 00:00:00 2001 From: Parnic Date: Wed, 18 Dec 2024 09:33:14 -0600 Subject: [PATCH] Day 18 This is slow and I'm not sure why. I should probably have used my Dijkstra utility, but I'm not convinced it's working right now. --- src/18.cs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/18.cs diff --git a/src/18.cs b/src/18.cs new file mode 100644 index 0000000..b436b5f --- /dev/null +++ b/src/18.cs @@ -0,0 +1,99 @@ +using aoc2024.Util; + +namespace aoc2024; + +internal class Day18 : Day +{ + private const int spaceSize = 71; + + private List startPoints = []; + private readonly ivec2 endPos = new(spaceSize - 1, spaceSize - 1); + + internal override void Parse() + { + startPoints = Util.Parsing.ReadAllLines($"{GetDay()}").Select(ivec2.Parse).ToList(); + } + + private int GetStepsToExit(bool[][] grid) + { + var q = new Queue<(ivec2 pos, int dist)>(); + q.Enqueue((ivec2.ZERO, 0)); + Dictionary visited = []; + while (q.TryDequeue(out var pt)) + { + if (visited.TryGetValue(pt.pos, out var dist) && dist <= pt.dist) + { + continue; + } + visited[pt.pos] = pt.dist; + + foreach (var point in pt.pos.GetBoundedOrthogonalNeighbors(0, 0, spaceSize - 1, spaceSize - 1)) + { + if (grid[point.y][point.x]) + { + continue; + } + + if (!visited.TryGetValue(point, out int dist2) || dist2 > pt.dist + 1) + { + q.Enqueue((point, pt.dist + 1)); + } + } + } + + if (!visited.TryGetValue(endPos, out int steps)) + { + return -1; + } + return steps; + } + + internal override string Part1() + { + bool[][] grid = new bool[spaceSize][]; + for (int i = 0; i < grid.Length; i++) + { + grid[i] = new bool[spaceSize]; + } + + const int numToRead = 1024; + for (int i = 0; i < numToRead; i++) + { + grid[startPoints[i].y][startPoints[i].x] = true; + } + + var shortestDist = GetStepsToExit(grid); + + return $"Minimum steps to exit: <+white>{shortestDist}"; + } + + internal override string Part2() + { + var endPos = new ivec2(spaceSize - 1, spaceSize - 1); + + bool[][] grid = new bool[spaceSize][]; + for (int i = 0; i < grid.Length; i++) + { + grid[i] = new bool[spaceSize]; + } + + const int numToRead = 1024; + int blockerIdx = -1; + for (int i = 0; i < numToRead; i++) + { + grid[startPoints[i].y][startPoints[i].x] = true; + } + for (int i = numToRead; i < startPoints.Count; i++) + { + grid[startPoints[i].y][startPoints[i].x] = true; + var steps = GetStepsToExit(grid); + if (steps == -1) + { + blockerIdx = i; + break; + } + } + + return $"Coordinates of exit-blocking corruption: <+white>{startPoints[blockerIdx]}"; + } +}