Skip to content

Commit

Permalink
Minor Performance Increase With Sequence<Point2D>
Browse files Browse the repository at this point in the history
Get a slight performance increase using sequences as opposed to List/Set for the antinodes return type
  • Loading branch information
GeistInDerSH committed Dec 8, 2024
1 parent e51aa60 commit 935a6aa
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/com/geistindersh/aoc/year2024/Day8.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ class Day8(
private val frequencies = grid.values.toSet().filterNot { it == '.' }

private fun antinodes(node: Char) =
buildSet {
sequence {
val points = grid.filterValues { it == node }.keys
for ((a, b) in points.pairCombinations()) {
val p = a - b
add(a + p)
add(b - p)
yield(a + p)
yield(b - p)
}
}

private fun antinodesRepeated(node: Char) =
buildSet {
sequence {
val points = grid.filterValues { it == node }.keys
addAll(points)
yieldAll(points)
for ((a, b) in points.pairCombinations()) {
val p = a - b

var incPoint = a + p
while (incPoint in grid) {
add(incPoint)
yield(incPoint)
incPoint += p
}

var decPoint = b - p
while (decPoint in grid) {
add(decPoint)
yield(decPoint)
decPoint -= p
}
}
Expand Down

0 comments on commit 935a6aa

Please sign in to comment.