Skip to content

Commit

Permalink
[2024/22] Refactor part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 23, 2024
1 parent 4fcf706 commit dab3b6a
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/main/kotlin/adventofcode/year2024/Day22MonkeyMarket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@ class Day22MonkeyMarket(customInput: PuzzleInput? = null) : Puzzle(customInput)

override fun partTwo() =
buildMap {
secretNumbers.forEach { secretNumber ->
val secrets = secretNumber.evolve(2000)
val changes = secrets.zipWithNext().map { (a, b) -> a % 10 - b % 10 }
val seen = mutableSetOf<String>()

(0..secrets.lastIndex - 4).forEach { i ->
val seq = changes.slice(i..i + 3).joinToString()

if (seq !in seen) {
compute(seq) { _, current -> secrets[i + 4] % 10 + (current ?: 0L) }
seen += seq
}
secretNumbers
.map { secretNumber -> secretNumber.evolve(2000).map { i -> i % 10 }.toList() }
.forEach { sequence ->
sequence
.windowed(5, 1)
.map { it.zipWithNext { a, b -> b - a } to it.last() }
.distinctBy { (a, _) -> a }
.forEach { (key, value) ->
this[key] = (this[key] ?: 0L) + value
}
}
}
}
.values
.max()
Expand Down

0 comments on commit dab3b6a

Please sign in to comment.