Skip to content

Commit

Permalink
aoc 2024 day 18 part 2 optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 18, 2024
1 parent 0c806e0 commit c4a2f2f
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions adventofcode/src/main/java/org/ck/adventofcode/year2024/Day18.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
name = "Day 18: RAM Run - Part 2",
url = "https://adventofcode.com/2024/day/18#part2",
category = "2024")
/// possible optimisations for part 2:
/// <pre>
/// - do a binary search on the remaining data packets
/// - only calculate next path if the new corrupted packed is on the last optimal path
/// </pre>
public class Day18 extends AOCSolution {

@Override
Expand All @@ -47,14 +42,22 @@ private void run(
}

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

boolean foundPath = initialFoundPath;

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

final Set<Coordinate> visited = new HashSet<>();
if (!previousPath.isEmpty() && !previousPath.contains(packets.get(offset))) {
foundPath = true;
++offset;
continue;
}

final Map<Coordinate, Coordinate> origins = new HashMap<>();
origins.put(new Coordinate(0, 0), null);

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

if (!visited.contains(coordinate)) {
visited.add(coordinate);

for (final Coordinate next :
Set.of(
new Coordinate(coordinate.x() + 1, coordinate.y()),
new Coordinate(coordinate.x(), coordinate.y() + 1),
new Coordinate(coordinate.x() - 1, coordinate.y()),
new Coordinate(coordinate.x(), coordinate.y() - 1))) {
if (next.x() >= 0 && next.y() >= 0 && next.x() <= gridSize && next.y() <= gridSize) {
if (!visited.contains(next) && !corrupted.contains(next)) {
queue.add(new State(next, current.count() + 1));
}
for (final Coordinate next :
Set.of(
new Coordinate(coordinate.x() + 1, coordinate.y()),
new Coordinate(coordinate.x(), coordinate.y() + 1),
new Coordinate(coordinate.x() - 1, coordinate.y()),
new Coordinate(coordinate.x(), coordinate.y() - 1))) {
if (next.x() >= 0 && next.y() >= 0 && next.x() <= gridSize && next.y() <= gridSize) {
if (!origins.containsKey(next) && !corrupted.contains(next)) {
origins.put(next, coordinate);
queue.add(new State(next, current.count() + 1));
}
}
}
}

Coordinate current = new Coordinate(gridSize, gridSize);
while (current != null) {
previousPath.add(current);
current = origins.get(current);
}

if (!foundPath && initialFoundPath) {
final Coordinate last = packets.get(offset);
print("%d,%d".formatted(last.x(), last.y()));
Expand Down

0 comments on commit c4a2f2f

Please sign in to comment.