Skip to content

Commit

Permalink
AoC 2023 Day 23 - java - faster
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 23, 2023
1 parent 08e81ab commit 4ac60dc
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/AoC2023_23.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.github.pareronia.aoc.CharGrid;
Expand Down Expand Up @@ -75,15 +76,26 @@ public int findLongestHikeLength() {
log(pois);
final Map<Cell, Set<Edge>> graph = this.buildGraph(pois, false);
log(graph);
return this.findLongest(graph, start, end, new HashSet<>());
final int distFromStart = graph.get(this.start).iterator().next().weight;
final int distToEnd = graph.get(this.end).iterator().next().weight;
final Cell newStart = graph.entrySet().stream()
.filter(e -> e.getValue().contains(new Edge(this.start, distFromStart)))
.map(Entry::getKey)
.findFirst().orElseThrow();
final Cell newEnd = graph.entrySet().stream()
.filter(e -> e.getValue().contains(new Edge(this.end, distToEnd)))
.map(Entry::getKey)
.findFirst().orElseThrow();
return distFromStart + distToEnd
+ this.findLongest(graph, newStart, newEnd, new HashSet<>());
}

public int findLongestHikeLengthWithDownwardSlopesOnly() {
final Set<Cell> pois = this.findPois();
log(pois);
final Map<Cell, Set<Edge>> graph = this.buildGraph(pois, true);
log(graph);
return this.findLongest(graph, start, end, new HashSet<>());
return this.findLongest(graph, this.start, this.end, new HashSet<>());
}

private int findLongest(
Expand Down

0 comments on commit 4ac60dc

Please sign in to comment.