Created
November 24, 2022 22:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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