Skip to content

Commit 31a5d58

Browse files
committed
blockset
1 parent 5278adc commit 31a5d58

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

2024/Day18/Solution.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace AdventOfCode.Y2024.Day18;
88
[ProblemName("RAM Run")]
99
class Solution : Solver {
1010

11-
public object PartOne(string input) =>
12-
Distance(GetBlocks(input), 1024);
11+
public object PartOne(string input) => Distance(GetBlocks(input).Take(1024));
1312

1413
public object PartTwo(string input) {
1514
// find the first block position that will cut off the goal position
@@ -19,7 +18,7 @@ public object PartTwo(string input) {
1918
var (lo, hi) = (0, blocks.Length);
2019
while (hi - lo > 1) {
2120
var m = (lo + hi) / 2;
22-
if (Distance(blocks, m) == null) {
21+
if (Distance(blocks.Take(m)) == null) {
2322
hi = m;
2423
} else {
2524
lo = m;
@@ -28,10 +27,10 @@ public object PartTwo(string input) {
2827
return $"{blocks[lo].Real},{blocks[lo].Imaginary}";
2928
}
3029

31-
int? Distance(Complex[] blocks, int take) {
30+
int? Distance(IEnumerable<Complex> blocks) {
3231
// our standard priority queue based path finding
3332

34-
var blocked = blocks.Take(take).ToHashSet();
33+
var blockSet = blocks.ToHashSet();
3534
var size = 70;
3635
var goal = size + size * Complex.ImaginaryOne;
3736
var q = new PriorityQueue<Complex, int>();
@@ -45,7 +44,7 @@ public object PartTwo(string input) {
4544
foreach (var dir in new[] { 1, -1, Complex.ImaginaryOne, -Complex.ImaginaryOne }) {
4645
var posT = pos + dir;
4746
if (!seen.Contains(posT) &&
48-
!blocked.Contains(posT) &&
47+
!blockSet.Contains(posT) &&
4948
0 <= posT.Imaginary && posT.Imaginary <= size &&
5049
0 <= posT.Real && posT.Real <= size
5150
) {

0 commit comments

Comments
 (0)