Skip to content

Commit

Permalink
Reduce Runtime to Sub 200ms
Browse files Browse the repository at this point in the history
Reduce the runtime from ~250ms to 180ms with better branching due to profiling
  • Loading branch information
GeistInDerSH committed Dec 16, 2024
1 parent 43ebc45 commit 49870fb
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main/kotlin/com/geistindersh/aoc/year2024/Day16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Day16(
val direction: Direction = Direction.East,
val score: Long = 0,
val path: List<Point2D> = emptyList(),
)
) {
fun generateKey() = position to direction
}

/**
* Get the minimum score required to traverse from [start] to [end]
Expand All @@ -37,10 +39,10 @@ class Day16(
while (queue.isNotEmpty()) {
val path = queue.poll()
if (path.position == end) return path.score
if (!seen.add(path.position to path.direction)) continue
if (!seen.add(path.generateKey())) continue

val next = path.position + path.direction
if (next in data && data[next]!! != '#') {
if (data[next]!! != '#') {
queue.add(path.copy(position = next, score = path.score + 1))
}

Expand Down Expand Up @@ -68,16 +70,18 @@ class Day16(
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

val key = path.generateKey()
val value = visited[key]
if (value != null && path.score > value) continue
if (value == null || path.score < value) visited[key] = path.score
if (path.position == end && path.score == minScore) {
shortestPathPoints.add(path.path)
continue
}

val next = path.position + path.direction
if (next in data && data[next]!! != '#') {
if (data[next]!! != '#') {
queue.add(path.copy(position = next, score = path.score + 1, path = path.path + path.position))
}

Expand Down

0 comments on commit 49870fb

Please sign in to comment.