Skip to content

Commit

Permalink
make day 17 faster by simpler key in map
Browse files Browse the repository at this point in the history
  • Loading branch information
zebalu committed Dec 17, 2023
1 parent 0d6350a commit 136e421
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
34 changes: 15 additions & 19 deletions aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day17.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
public class Day17 {
public static void main(String[] args) {
List<String> maze = readInput().lines().toList();
part1(maze);
part2(maze);
System.out.println(part1(maze));
System.out.println(part2(maze));
}

private static void part1(List<String> maze) {
System.out.println(minHeatLoss(maze, 3, (a, b) -> true));
private static int part1(List<String> maze) {
return minHeatLoss(maze, 3, (a, b) -> true);
}

private static void part2(List<String> maze) {
System.out
.println(minHeatLoss(maze, 10, (c, s) -> 4 <= s.straightLength() || s.pos().directionOf(c) == s.dir()));
private static int part2(List<String> maze) {
return minHeatLoss(maze, 10, (c, s) -> 4 <= s.straightLength() || s.pos().directionOf(c) == s.dir());
}

private static int minHeatLoss(List<String> maze, int maxLength, BiPredicate<Coord, State> nextFilter) {
Expand All @@ -26,7 +25,7 @@ private static int minHeatLoss(List<String> maze, int maxLength, BiPredicate<Coo
Predicate<Coord> isValidCood = c -> 0 <= c.x() && 0 <= c.y() && c.x < width && c.y < height;
Predicate<State> isValidState = s -> isValidCood.test(s.pos()) && s.straightLength() <= maxLength;
Coord target = new Coord(width - 1, height - 1);
Map<PosStepDir, Integer> psdCost = new HashMap<>();
Map<Long, Integer> bestCost = new HashMap<>();
Queue<State> queue = new PriorityQueue<>();
queue.add(new State(new Coord(0, 0), 0, 0, Direction.EAST));
while (!queue.isEmpty()) {
Expand All @@ -40,9 +39,9 @@ private static int minHeatLoss(List<String> maze, int maxLength, BiPredicate<Coo
if (s.pos().equals(target)) {
return s.heatLoss;
}
var psd = PosStepDir.of(s);
if (!psdCost.containsKey(psd) || s.heatLoss < psdCost.get(psd)) {
psdCost.put(psd, s.heatLoss);
var costKey = s.toKey();
if (!bestCost.containsKey(costKey) || s.heatLoss < bestCost.get(costKey)) {
bestCost.put(costKey, s.heatLoss);
queue.add(s);
}
}
Expand Down Expand Up @@ -96,8 +95,7 @@ Direction directionOf(Coord coord) {

private record State(Coord pos, int straightLength, int heatLoss, Direction dir) implements Comparable<State> {

private static final Comparator<State> COMPARATOR = Comparator.comparingLong(State::heatLoss)
.thenComparingInt(State::straightLength);
private static final Comparator<State> COMPARATOR = Comparator.comparingLong(State::heatLoss).thenComparingInt(State::straightLength);

@Override
public int compareTo(State o) {
Expand All @@ -110,12 +108,10 @@ int nextStraight(Coord c) {
}
return 1;
}

}

private record PosStepDir(Coord pos, int steps, Direction dir) {
static PosStepDir of(State state) {
return new PosStepDir(state.pos, state.straightLength, state.dir);

long toKey() {
return 0L+pos.x()*1_000_000L+pos.y()*1_000L+straightLength*10L+dir.ordinal();
}

}
}
2 changes: 2 additions & 0 deletions aoc2023/src/main/java/io/github/zebalu/aoc2023/main/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.github.zebalu.aoc2023.days.Day14;
import io.github.zebalu.aoc2023.days.Day15;
import io.github.zebalu.aoc2023.days.Day16;
import io.github.zebalu.aoc2023.days.Day17;

public class App {

Expand Down Expand Up @@ -59,6 +60,7 @@ public static void main(String[] args) {
exec(new DisplayData(14, "Parabolic Reflector Dish", Day14::main));
exec(new DisplayData(15, "Lens Library", Day15::main));
exec(new DisplayData(16, "The Floor Will Be Lava", Day16::main));
exec(new DisplayData(17, "Clumsy Crucible", Day17::main));
Instant end = Instant.now();
System.out.println("so far:\t"+Duration.between(start, end).toMillis()+" ms...");
}
Expand Down

0 comments on commit 136e421

Please sign in to comment.