diff --git a/lib/src/main/kotlin/de/linkel/aoc/utils/repeatCycle/Repetition.kt b/lib/src/main/kotlin/de/linkel/aoc/utils/repeatCycle/Repetition.kt index b6734b1..02bd970 100644 --- a/lib/src/main/kotlin/de/linkel/aoc/utils/repeatCycle/Repetition.kt +++ b/lib/src/main/kotlin/de/linkel/aoc/utils/repeatCycle/Repetition.kt @@ -4,43 +4,14 @@ class RepetitionContext( 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 + private val maxHashes: Int = 0 ) { - fun withHashing(lambda: (state: T) -> HO): RepetitionContext { + fun withMemory(memorySize: Int, lambda: (state: T) -> HO): RepetitionContext { return RepetitionContext( iterations = this.iterations, transformation = this.transformation, hash = lambda, - maxHashes = this.maxHashes, - iterationsBeforeHashing = this.iterationsBeforeHashing - ) - } - fun withMemorySize(value: Int): RepetitionContext { - return RepetitionContext( - iterations = this.iterations, - transformation = this.transformation, - hash = this.hash, - maxHashes = value, - iterationsBeforeHashing = this.iterationsBeforeHashing - ) - } - fun withMinimumTransformations(value: Int): RepetitionContext { - return RepetitionContext( - iterations = this.iterations, - transformation = this.transformation, - hash = this.hash, - maxHashes = this.maxHashes, - iterationsBeforeHashing = value - ) - } - fun withIterations(value: Int): RepetitionContext { - return RepetitionContext( - iterations = value, - transformation = this.transformation, - hash = this.hash, - maxHashes = this.maxHashes, - iterationsBeforeHashing = value + maxHashes = memorySize, ) } @@ -51,7 +22,7 @@ class RepetitionContext( val hashLambda = hash while (i < iterations) { state = transformation(state) - if (iterationsBeforeHashing > i && hashLambda != null) { + if (hashLambda != null) { val h = hashLambda(state) val hi = lastIterations.indexOf(h) if (hi > -1) { @@ -72,3 +43,6 @@ class RepetitionContext( return state } } + +fun Int.repeatedTransformations(transformation: (state: T) -> T): RepetitionContext + = RepetitionContext(this, transformation)