From cc2b166e23c18f990ba5cd746ed1af84655cfbdf Mon Sep 17 00:00:00 2001 From: Parnic Date: Sat, 24 Dec 2022 14:40:05 -0600 Subject: [PATCH] Day 24 optimized This takes the runtime from 5-6s on part 2 to ~100ms by using a better data structure for lookups than trying to check if the Queue contains an existing state or not. Now the slowest thing here is the GetNeighbors loop. I feel like that can be improved somehow, but I'm not sure if I can improve it and still retain its ergonomics or not. We shall see! --- src/24.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/24.cs b/src/24.cs index ee0e462..34a730c 100644 --- a/src/24.cs +++ b/src/24.cs @@ -192,6 +192,7 @@ internal class Day24 : Day private static int getMinSteps(ivec2 start, ivec2 dest, int startGridState = 0) { Queue<(ivec2 pos, int steps)> states = new(); + HashSet visited = new(); states.Enqueue((start, 0)); int minSteps = int.MaxValue; @@ -208,10 +209,11 @@ internal class Day24 : Day // check if we can wait if (next[q.pos.y][q.pos.x] == (int) cellType.open) { - var nextState = (pos: q.pos, steps: q.steps + 1); - if (!states.Contains(nextState)) + var visitedVec = new ivec3(q.pos.x, q.pos.y, q.steps + 1); + if (!visited.Contains(visitedVec)) { - states.Enqueue(nextState); + states.Enqueue((q.pos, q.steps + 1)); + visited.Add(visitedVec); } } @@ -235,10 +237,11 @@ internal class Day24 : Day if (next[n.y][n.x] == (int) cellType.open) { - var nextState = (pos: n, steps: q.steps + 1); - if (!states.Contains(nextState)) + var visitedVec = new ivec3(n.x, n.y, q.steps + 1); + if (!visited.Contains(visitedVec)) { - states.Enqueue(nextState); + states.Enqueue((n, q.steps + 1)); + visited.Add(visitedVec); } } }