-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper class for repetitions of cyclic transformations
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
lib/src/main/kotlin/de/linkel/aoc/utils/repeatCycle/Repetition.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package de.linkel.aoc.utils.repeatCycle | ||
|
||
class RepetitionContext<T, H>( | ||
private val iterations: Int, | ||
private val transformation: (state: T) -> T, | ||
private val hash: ((state: T) -> H)? = null, | ||
private val maxHashes: Int = 100, | ||
private val iterationsBeforeHashing: Int = 50 | ||
) { | ||
fun <HO> withHashing(lambda: (state: T) -> HO): RepetitionContext<T, HO> { | ||
return RepetitionContext<T, HO>( | ||
iterations = this.iterations, | ||
transformation = this.transformation, | ||
hash = lambda, | ||
maxHashes = this.maxHashes, | ||
iterationsBeforeHashing = this.iterationsBeforeHashing | ||
) | ||
} | ||
fun withMemorySize(value: Int): RepetitionContext<T, H> { | ||
return RepetitionContext( | ||
iterations = this.iterations, | ||
transformation = this.transformation, | ||
hash = this.hash, | ||
maxHashes = value, | ||
iterationsBeforeHashing = this.iterationsBeforeHashing | ||
) | ||
} | ||
fun withMinimumTransformations(value: Int): RepetitionContext<T, H> { | ||
return RepetitionContext( | ||
iterations = this.iterations, | ||
transformation = this.transformation, | ||
hash = this.hash, | ||
maxHashes = this.maxHashes, | ||
iterationsBeforeHashing = value | ||
) | ||
} | ||
fun withIterations(value: Int): RepetitionContext<T, H> { | ||
return RepetitionContext( | ||
iterations = value, | ||
transformation = this.transformation, | ||
hash = this.hash, | ||
maxHashes = this.maxHashes, | ||
iterationsBeforeHashing = value | ||
) | ||
} | ||
|
||
fun transform(input: T): T { | ||
val lastIterations = mutableListOf<H>() | ||
var i = 0 | ||
var state: T = input | ||
val hashLambda = hash | ||
while (i < iterations) { | ||
state = transformation(state) | ||
if (iterationsBeforeHashing > i && hashLambda != null) { | ||
val h = hashLambda(state) | ||
val hi = lastIterations.indexOf(h) | ||
if (hi > -1) { | ||
val cycleLength = hi + 1 | ||
while (i + cycleLength < iterations) { | ||
i += cycleLength | ||
} | ||
|
||
lastIterations.clear() | ||
} | ||
lastIterations.add(0, h) | ||
if (lastIterations.size > maxHashes) { | ||
lastIterations.removeAt(maxHashes) | ||
} | ||
} | ||
i++ | ||
} | ||
return state | ||
} | ||
} |