Skip to content

Commit

Permalink
Make sure scrambles within one round are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorbg committed Jul 22, 2022
1 parent b65a4f8 commit 64d6b60
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ enum class PuzzleData(private val registry: PuzzleRegistry) {
}

fun generateEfficientScrambles(num: Int, action: (String) -> Unit = {}): List<String> {
return List(num) {
yieldScramble().also(action)
}
return generateSequence { yieldScramble() }
.distinct() // TODO ideally detect isomorphic states too, but most puzzles don't support rotations rn
.onEach(action)
.take(num)
.toList()
}

private fun yieldScramble() = SCRAMBLE_CACHERS[this.key]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ object WCIFScrambleMatcher {
fun decryptScrambleSets(wcif: Competition, password: String) =
decryptScrambleSets(wcif, SymmetricCipher.generateKey(password))

private fun cryptScrambleSets(wcif: Competition, cipherKey: Key, cipherOpMode: Int, cipherMethod: (Cipher, String) -> String): Competition {
private fun cryptScrambleSets(
wcif: Competition,
cipherKey: Key,
cipherOpMode: Int,
cipherMethod: (Cipher, String) -> String
): Competition {
val initedCipherInstance = SymmetricCipher.CIPHER_INSTANCE
.apply { init(cipherOpMode, cipherKey) }

Expand All @@ -43,8 +48,10 @@ object WCIFScrambleMatcher {
val scrambledEvents = wcif.events.map { e ->
val scrambledRounds = e.rounds.map { r ->
val cryptedSets = r.scrambleSets.map { scr ->
val cryptedStdScrambles = applyCipherToScrambles(scr.scrambles) { cipherMethod(initedCipherInstance, it) }
val cryptedExtraScrambles = applyCipherToScrambles(scr.extraScrambles) { cipherMethod(initedCipherInstance, it) }
val cryptedStdScrambles =
applyCipherToScrambles(scr.scrambles) { cipherMethod(initedCipherInstance, it) }
val cryptedExtraScrambles =
applyCipherToScrambles(scr.extraScrambles) { cipherMethod(initedCipherInstance, it) }

scr.copy(scrambles = cryptedStdScrambles, extraScrambles = cryptedExtraScrambles)
}
Expand Down Expand Up @@ -76,7 +83,7 @@ object WCIFScrambleMatcher {
}

// helper fn for synchronously running tests
fun fillScrambleSets(wcif: Competition) = runBlocking { fillScrambleSetsAsync(wcif) { _, _ -> Unit } }
fun fillScrambleSets(wcif: Competition) = runBlocking { fillScrambleSetsAsync(wcif) { _, _ -> } }

private suspend fun scrambleRound(round: Round, onUpdate: (EventData, String) -> Unit): Round {
val scrambles = coroutineScope {
Expand All @@ -90,27 +97,52 @@ object WCIFScrambleMatcher {
val eventModel = round.idCode.eventModel
?: ScrambleMatchingException.error("Unable to load scrambler for Round ${round.idCode}")

val standardScrambleNum = standardScrambleCountPerSet(round)
val puzzle = eventModel.scrambler
val totalScrambleNum = totalScrambleCountPerSet(round)

// generate scrambles for a round all at once to allow for easy duplicate checking
val rawScrambles = puzzle.generateEfficientScrambles(totalScrambleNum) { onUpdate(eventModel, it) }

val scrambles = wrapRawScrambles(rawScrambles, round, false)
val extraScrambles = wrapRawScrambles(rawScrambles, round, true)

val scrambles = if (eventModel == EventData.THREE_MULTI_BLD) {
val countPerAttempt = standardScrambleNum / round.expectedAttemptNum
// dummy ID -- indexing happens afterwards
return ScrambleSet(ID_PENDING, scrambles, extraScrambles)
}

private fun wrapRawScrambles(rawScrambles: List<String>, round: Round, isExtra: Boolean = false): List<Scramble> {
val standardScrambleNum = standardScrambleCountPerSet(round)
val extraScrambleNum = extraScrambleCountPerSet(round)

List(round.expectedAttemptNum) { _ ->
val scrambles = puzzle.generateEfficientScrambles(countPerAttempt) { onUpdate(eventModel, it) }
if (round.idCode.eventModel == EventData.THREE_MULTI_BLD) {
val standardCountPerAttempt = standardScrambleNum / round.expectedAttemptNum
val extraCountPerAttempt = extraScrambleNum / round.expectedAttemptNum

return List(round.expectedAttemptNum) { n ->
val scrambles = rawScrambles
// drop previous attempts first
.drop(n * (standardCountPerAttempt + extraCountPerAttempt))
// then slice the relevant scrambles *within* the current attempt
.sliceScrambles(standardCountPerAttempt, extraCountPerAttempt, isExtra)
.joinToString(Scramble.WCIF_NEWLINE_CHAR)

Scramble(scrambles)
}
} else {
puzzle.generateEfficientScrambles(standardScrambleNum) { onUpdate(eventModel, it) }.map(::Scramble)
return rawScrambles.sliceScrambles(standardScrambleNum, extraScrambleNum, isExtra)
.map(::Scramble)
}
}

val extraScrambleNum = extraScrambleCountPerSet(round)
val extraScrambles = puzzle.generateEfficientScrambles(extraScrambleNum) { onUpdate(eventModel, it) }.map(::Scramble)

// dummy ID -- indexing happens afterwards
return ScrambleSet(ID_PENDING, scrambles, extraScrambles)
private fun <T> List<T>.sliceScrambles(standardCount: Int, extraCount: Int, isExtra: Boolean = false): List<T> {
// This is a bit wild. We generate *all* scrambles for a round at once,
// in order to be able to easily check for duplicates during generation.
// But in the WCIF model, we want to split "standard" scrambles from extra scrambles.
return this
// so if we want extra scrambles, we skip over the standard scrambles
.drop(if (isExtra) standardCount else 0)
// and then we either take the remaining extra scrambles or the first N standard scrambles.
.take(if (isExtra) extraCount else standardCount)
}

private fun standardScrambleCountPerSet(round: Round): Int {
Expand All @@ -125,8 +157,13 @@ object WCIFScrambleMatcher {
}

private fun extraScrambleCountPerSet(round: Round): Int {
return round.findExtension<ExtraScrambleCountExtension>()?.extraAttempts
val baseExtraCount = round.findExtension<ExtraScrambleCountExtension>()?.extraAttempts
?: defaultExtraCount(round.idCode.eventModel)

if (round.idCode.eventModel == EventData.THREE_MULTI_BLD)
return baseExtraCount * round.expectedAttemptNum

return baseExtraCount
}

private fun defaultExtraCount(event: EventData?): Int {
Expand All @@ -141,7 +178,7 @@ object WCIFScrambleMatcher {
fun getScrambleCountsPerEvent(wcif: Competition): Map<String, Int> {
return wcif.events.associateWith { it.rounds }
.mapValues { (_, rs) ->
rs.map { it.scrambleSetCount * totalScrambleCountPerSet(it) }.sum()
rs.sumOf { it.scrambleSetCount * totalScrambleCountPerSet(it) }
}.mapKeys { it.key.id }
}

Expand Down Expand Up @@ -221,8 +258,7 @@ object WCIFScrambleMatcher {

private fun <T : IndexingIdProvider> buildReindexingMap(candidates: List<T>): Map<T, Int> {
val forReindexing = candidates.filter { it.id == ID_PENDING }
val maxAssignedId = (candidates - forReindexing)
.map { it.id }.maxOrNull() ?: ID_PENDING
val maxAssignedId = (candidates - forReindexing).maxOfOrNull { it.id } ?: ID_PENDING

return forReindexing.mapIndexed { i, elem -> elem to maxAssignedId + 1 + i }
.toMap()
Expand Down Expand Up @@ -280,7 +316,12 @@ object WCIFScrambleMatcher {
val copiedActCode = activity.activityCode.copyParts(groupNumber = it)
val childSetId = matchedRound.scrambleSets[it].id

activity.copy(id = ID_PENDING, activityCode = copiedActCode, childActivities = listOf(), scrambleSetId = childSetId)
activity.copy(
id = ID_PENDING,
activityCode = copiedActCode,
childActivities = listOf(),
scrambleSetId = childSetId
)
}

return activity.copy(childActivities = inventedChildren)
Expand Down

0 comments on commit 64d6b60

Please sign in to comment.