Skip to content

Commit c4a2f2f

Browse files
committed
aoc 2024 day 18 part 2 optimisation
1 parent 0c806e0 commit c4a2f2f

File tree

1 file changed

+25
-19
lines changed
  • adventofcode/src/main/java/org/ck/adventofcode/year2024

1 file changed

+25
-19
lines changed

adventofcode/src/main/java/org/ck/adventofcode/year2024/Day18.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
name = "Day 18: RAM Run - Part 2",
1717
url = "https://adventofcode.com/2024/day/18#part2",
1818
category = "2024")
19-
/// possible optimisations for part 2:
20-
/// <pre>
21-
/// - do a binary search on the remaining data packets
22-
/// - only calculate next path if the new corrupted packed is on the last optimal path
23-
/// </pre>
2419
public class Day18 extends AOCSolution {
2520

2621
@Override
@@ -47,14 +42,22 @@ private void run(
4742
}
4843

4944
final Set<Coordinate> corrupted = packets.stream().limit(offset).collect(Collectors.toSet());
45+
final Set<Coordinate> previousPath = new HashSet<>();
5046

5147
boolean foundPath = initialFoundPath;
5248

5349
while (shouldRun.test(foundPath)) {
5450
foundPath = false;
5551
corrupted.add(packets.get(offset));
5652

57-
final Set<Coordinate> visited = new HashSet<>();
53+
if (!previousPath.isEmpty() && !previousPath.contains(packets.get(offset))) {
54+
foundPath = true;
55+
++offset;
56+
continue;
57+
}
58+
59+
final Map<Coordinate, Coordinate> origins = new HashMap<>();
60+
origins.put(new Coordinate(0, 0), null);
5861

5962
final Queue<State> queue = new PriorityQueue<>(Comparator.comparingInt(State::count));
6063
queue.add(new State(new Coordinate(0, 0), 0));
@@ -71,24 +74,27 @@ private void run(
7174
break;
7275
}
7376

74-
if (!visited.contains(coordinate)) {
75-
visited.add(coordinate);
76-
77-
for (final Coordinate next :
78-
Set.of(
79-
new Coordinate(coordinate.x() + 1, coordinate.y()),
80-
new Coordinate(coordinate.x(), coordinate.y() + 1),
81-
new Coordinate(coordinate.x() - 1, coordinate.y()),
82-
new Coordinate(coordinate.x(), coordinate.y() - 1))) {
83-
if (next.x() >= 0 && next.y() >= 0 && next.x() <= gridSize && next.y() <= gridSize) {
84-
if (!visited.contains(next) && !corrupted.contains(next)) {
85-
queue.add(new State(next, current.count() + 1));
86-
}
77+
for (final Coordinate next :
78+
Set.of(
79+
new Coordinate(coordinate.x() + 1, coordinate.y()),
80+
new Coordinate(coordinate.x(), coordinate.y() + 1),
81+
new Coordinate(coordinate.x() - 1, coordinate.y()),
82+
new Coordinate(coordinate.x(), coordinate.y() - 1))) {
83+
if (next.x() >= 0 && next.y() >= 0 && next.x() <= gridSize && next.y() <= gridSize) {
84+
if (!origins.containsKey(next) && !corrupted.contains(next)) {
85+
origins.put(next, coordinate);
86+
queue.add(new State(next, current.count() + 1));
8787
}
8888
}
8989
}
9090
}
9191

92+
Coordinate current = new Coordinate(gridSize, gridSize);
93+
while (current != null) {
94+
previousPath.add(current);
95+
current = origins.get(current);
96+
}
97+
9298
if (!foundPath && initialFoundPath) {
9399
final Coordinate last = packets.get(offset);
94100
print("%d,%d".formatted(last.x(), last.y()));

0 commit comments

Comments
 (0)