Skip to content

Commit

Permalink
2x speedup; still over 5 seconds...
Browse files Browse the repository at this point in the history
  • Loading branch information
zebalu committed Dec 23, 2023
1 parent 56a2c3f commit 0a33df9
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day23.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ List<Coord> part2Neighbours(List<String> maze) {
return result;
}

@Override
public int hashCode() {
return x*10_000+y;
}

private static void appendIfMatch(List<Coord> collector, List<String> maze, Coord coord, char accepted) {
char at = coord.extractAny(maze);
if(at == '.' || at == accepted) {
Expand Down Expand Up @@ -175,28 +180,33 @@ int longestPath(Coord start, Coord target) {
BitSet startBs = new BitSet(forks.size());
startBs.set(forkIds.get(start));
stack.add(new Step(startId, startBs, 0));
int longest = 0;
int[] longest = new int[] {0};
while(!stack.isEmpty()) {
Step curr = stack.pop();
BitSet cons = connections.get(curr.last);
for(int i=0; i<cons.size(); ++i) {
if(cons.get(i) && !curr.history.get(i)) {
cons.stream().forEach(i -> {
if (cons.get(i) && !curr.history.get(i)) {
int newCost = curr.length + costs.get(new Edge(curr.last, i));
if(i == targetId && longest < newCost ) {
longest = newCost;
if (i == targetId && longest[0] < newCost) {
longest[0] = newCost;
} else {
BitSet nextSet = (BitSet)curr.history.clone();
BitSet nextSet = (BitSet) curr.history.clone();
nextSet.set(i);
Step nextStep = new Step(i, nextSet, newCost);
stack.push(nextStep);
}
}
}
});
}
return longest;
return longest[0];
}

private record Step(int last, BitSet history, int length) {}
private record Edge(int from, int to) {}
private record Edge(int from, int to) {
@Override
public int hashCode() {
return from*10_000+to;
}
}
}
}

0 comments on commit 0a33df9

Please sign in to comment.