mirror of
https://github.com/parnic/advent-of-code-2022.git
synced 2025-06-16 13:40:13 -05:00
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!
This commit is contained in:
15
src/24.cs
15
src/24.cs
@ -192,6 +192,7 @@ internal class Day24 : Day
|
|||||||
private static int getMinSteps(ivec2 start, ivec2 dest, int startGridState = 0)
|
private static int getMinSteps(ivec2 start, ivec2 dest, int startGridState = 0)
|
||||||
{
|
{
|
||||||
Queue<(ivec2 pos, int steps)> states = new();
|
Queue<(ivec2 pos, int steps)> states = new();
|
||||||
|
HashSet<ivec3> visited = new();
|
||||||
states.Enqueue((start, 0));
|
states.Enqueue((start, 0));
|
||||||
|
|
||||||
int minSteps = int.MaxValue;
|
int minSteps = int.MaxValue;
|
||||||
@ -208,10 +209,11 @@ internal class Day24 : Day
|
|||||||
// check if we can wait
|
// check if we can wait
|
||||||
if (next[q.pos.y][q.pos.x] == (int) cellType.open)
|
if (next[q.pos.y][q.pos.x] == (int) cellType.open)
|
||||||
{
|
{
|
||||||
var nextState = (pos: q.pos, steps: q.steps + 1);
|
var visitedVec = new ivec3(q.pos.x, q.pos.y, q.steps + 1);
|
||||||
if (!states.Contains(nextState))
|
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)
|
if (next[n.y][n.x] == (int) cellType.open)
|
||||||
{
|
{
|
||||||
var nextState = (pos: n, steps: q.steps + 1);
|
var visitedVec = new ivec3(n.x, n.y, q.steps + 1);
|
||||||
if (!states.Contains(nextState))
|
if (!visited.Contains(visitedVec))
|
||||||
{
|
{
|
||||||
states.Enqueue(nextState);
|
states.Enqueue((n, q.steps + 1));
|
||||||
|
visited.Add(visitedVec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user