Skip to content

Commit

Permalink
Move Code to Shared Location
Browse files Browse the repository at this point in the history
Move some code that can be used in other parts to a shared location
  • Loading branch information
GeistInDerSH committed Dec 28, 2024
1 parent b1a0b7f commit 4b1a67d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/kotlin/com/geistindersh/aoc/helper/iterators/List.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ fun <T> Collection<T>.pairCombinations() =
}
}

/**
* Generate all possible pairs of the given collection, including duplicates
* @return A List of Pairs of the items in the collection
*/
fun <T> Collection<T>.generatePairs() = this.flatMap { i -> this.map { j -> Pair(i, j) } }

fun <T> List<T>.pairCombinationsNonInvertible() =
sequence {
val seen = mutableSetOf<Pair<T, T>>()
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/geistindersh/aoc/year2024/Day21.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.geistindersh.aoc.helper.enums.Direction
import com.geistindersh.aoc.helper.enums.Point2D
import com.geistindersh.aoc.helper.files.DataFile
import com.geistindersh.aoc.helper.files.fileToStream
import com.geistindersh.aoc.helper.iterators.generatePairs
import com.geistindersh.aoc.helper.report
import java.util.PriorityQueue
import kotlin.collections.count
Expand Down Expand Up @@ -62,8 +63,6 @@ class Day21(
private val NUMPAD_PATHS = NUMPAD.generatePaths()
private val DIRECTION_PAD_PATHS = DIRECTION_PAD.generatePaths()

private fun <T> Collection<T>.generatePairs() = this.flatMap { start -> this.map { end -> start to end } }

private fun Map<Point2D, Char>.generatePaths() =
this.keys
.generatePairs()
Expand Down

0 comments on commit 4b1a67d

Please sign in to comment.