Skip to content

Commit a812197

Browse files
committed
one less hashset
1 parent 31a5d58 commit a812197

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

2024/Day18/Solution.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace AdventOfCode.Y2024.Day18;
44
using System.Linq;
55
using System.Text.RegularExpressions;
66
using System.Numerics;
7+
using AngleSharp.Common;
78

89
[ProblemName("RAM Run")]
910
class Solution : Solver {
@@ -30,27 +31,25 @@ public object PartTwo(string input) {
3031
int? Distance(IEnumerable<Complex> blocks) {
3132
// our standard priority queue based path finding
3233

33-
var blockSet = blocks.ToHashSet();
3434
var size = 70;
35-
var goal = size + size * Complex.ImaginaryOne;
35+
var (start, goal) = (0, size + size * Complex.ImaginaryOne);
36+
var blocked = blocks.Concat(start).ToHashSet();
37+
3638
var q = new PriorityQueue<Complex, int>();
37-
q.Enqueue(0, 0);
38-
var seen = new HashSet<Complex>(0);
39-
39+
q.Enqueue(start, 0);
4040
while (q.TryDequeue(out var pos, out var dist)) {
4141
if (pos == goal) {
4242
return dist;
43-
} else {
44-
foreach (var dir in new[] { 1, -1, Complex.ImaginaryOne, -Complex.ImaginaryOne }) {
45-
var posT = pos + dir;
46-
if (!seen.Contains(posT) &&
47-
!blockSet.Contains(posT) &&
48-
0 <= posT.Imaginary && posT.Imaginary <= size &&
49-
0 <= posT.Real && posT.Real <= size
50-
) {
51-
q.Enqueue(posT, dist + 1);
52-
seen.Add(posT);
53-
}
43+
}
44+
45+
foreach (var dir in new[] { 1, -1, Complex.ImaginaryOne, -Complex.ImaginaryOne }) {
46+
var posT = pos + dir;
47+
if (!blocked.Contains(posT) &&
48+
0 <= posT.Imaginary && posT.Imaginary <= size &&
49+
0 <= posT.Real && posT.Real <= size
50+
) {
51+
q.Enqueue(posT, dist + 1);
52+
blocked.Add(posT);
5453
}
5554
}
5655
}

0 commit comments

Comments
 (0)