Skip to content

Commit

Permalink
AoC 2024 Day 16 Code Cleanup
Browse files Browse the repository at this point in the history
Cleanup function names to better reflect what is being done, along with the type signature
  • Loading branch information
GeistInDerSH committed Dec 16, 2024
1 parent 36afaf3 commit 43ebc45
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/main/kotlin/com/geistindersh/aoc/year2024/Day16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ class Day16(
val path: List<Point2D> = emptyList(),
)

private fun shortestPathScore(): Long {
/**
* Get the minimum score required to traverse from [start] to [end]
* @param start The starting point
* @param end The ending point
* @return The minimum score from [start] to [end]
*/
private fun getScoreOfShortestPath(
start: Point2D,
end: Point2D,
): Long {
val queue = PriorityQueue<Path>(compareBy { it.score }).apply { add(Path(start)) }
val seen = mutableSetOf<Pair<Point2D, Direction>>()
while (queue.isNotEmpty()) {
Expand All @@ -42,19 +51,28 @@ class Day16(
return -1L
}

private fun pointsForShortestPaths(): Set<Point2D> {
val minScore = shortestPathScore()
/**
* Get all possible shortest paths from [start] to [end]
* @param start The starting point
* @param end The ending point
* @return The shortest paths
*/
private fun getShortestPaths(
start: Point2D,
end: Point2D,
): Set<List<Point2D>> {
val minScore = getScoreOfShortestPath(start, end)
val queue = ArrayDeque<Path>().apply { add(Path(start)) }
val visited = mutableMapOf<Pair<Point2D, Direction>, Long>()
val shortestPathPoints = mutableSetOf<Point2D>(end)
val shortestPathPoints = mutableSetOf(listOf(end))
while (queue.isNotEmpty()) {
val path = queue.removeFirst()
if (path.score > minScore) continue
val key = path.position to path.direction
if (key in visited && visited[key]!! < path.score) continue
visited[key] = path.score
if (path.position == end && path.score == minScore) {
shortestPathPoints.addAll(path.path)
shortestPathPoints.add(path.path)
continue
}

Expand All @@ -70,9 +88,9 @@ class Day16(
return shortestPathPoints
}

fun part1() = shortestPathScore()
fun part1() = getScoreOfShortestPath(start, end)

fun part2() = pointsForShortestPaths().count()
fun part2() = getShortestPaths(start, end).flatMapTo(mutableSetOf<Point2D>()) { it }.count()
}

fun day16() {
Expand Down

0 comments on commit 43ebc45

Please sign in to comment.