Skip to content

Instantly share code, notes, and snippets.

@MaedehJJ
Created November 24, 2022 22:16
Show Gist options
  • Save MaedehJJ/c25a53ae80c2bf8e967794e682ae7bf5 to your computer and use it in GitHub Desktop.
Save MaedehJJ/c25a53ae80c2bf8e967794e682ae7bf5 to your computer and use it in GitHub Desktop.
The result of what happens when a developer couple's curiosity sparks while playing Spot it.
val group = listOf('a', 'b', 'c', 'd', 'e','f','g','h','i')
fun main() {
val potentialCards = getPotentialCards(4, group)
println(potentialCards)
println(potentialCards.size)
val builtCards = selectCards(potentialCards)
println(builtCards)
println(builtCards.size)
}
private fun selectCards(cards: Set<Set<Char>>): Set<Set<Char>> {
val diff = (cards.firstOrNull()?.size ?: 0) - 1
val savedCards = mutableSetOf<Set<Char>>()
cards.forEach { card ->
if (savedCards.all { saved -> (card - saved).size == diff}) {
savedCards.add(card)
}
}
return savedCards
}
private fun getPotentialCards(slots: Int, options: List<Char>): Set<Set<Char>> {
if (slots == 0) {
return setOf(setOf())
}
return options.map { option ->
getPotentialCards(slots - 1, options.filterNot { it == option }).map { subset ->
setOf(option) + subset
}
}.flatten().toSet()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment