Skip to content

Commit

Permalink
2024 - Day 18 - part 2 - added some comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 18, 2024
1 parent c80ec75 commit 98648ee
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/kotlin/no/rodland/advent_2024/Day18.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class Day18(val input: List<String>, private val bytesToTake: Int, maxIndex: Int
return parsed[findFirstPassingIndex - 1]
}

@Suppress("unused")
private fun binarySearchFromKotlin(): Pos {
// using kotlin's built in binary search
val hopp = (1..parsed.size)
val idx = (1..parsed.size)
.map { parsed.take(it) }
.binarySearch(bytesToTake) {
if (bfs(it.size, it.toSet()).isNotEmpty()) -1 else 1
}
return parsed[(hopp + 1) * -1]
return parsed[(idx + 1) * -1]
}

private fun binarySearch(left: Int, right: Int, test: (index: Int) -> Boolean): Int {
Expand All @@ -45,6 +46,7 @@ class Day18(val input: List<String>, private val bytesToTake: Int, maxIndex: Int
}
}

@Suppress("ConvertCallChainIntoSequence")
private fun bfs(n: Int, set: Set<Pos> = parsed.take(n).toSet()) = bfs(start, end, { p ->
p.neighbourCellsUDLR()
.filter { it !in set }
Expand Down

0 comments on commit 98648ee

Please sign in to comment.