Skip to content

Commit

Permalink
Add Day22
Browse files Browse the repository at this point in the history
  • Loading branch information
derNiklaas committed Dec 22, 2024
1 parent 9c909a6 commit 8d1c806
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/main/kotlin/Day22.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import utils.AoCDay
import utils.cartesianSquare
import utils.mapToLong

class Day22 : AoCDay() {
val numbers = input.mapToLong()
override fun part1(): Any {
return numbers.sumOf {
var result = it
repeat(2000) {
result = calculateNumber(result)
}
result
}
}

override fun part2(): Any {
val priceChanges = numbers.map {
val prices = mutableListOf(it % 10)
var result = it
repeat(2000) {
result = calculateNumber(result)
prices += result % 10
}

prices.windowed(2).map { (a, b) -> b - a to b }.windowed(4).map { l ->
l.map { it.first } to l.last().second
}.reversed().associate { it }
}
val allSequences = (-9L..9L).cartesianSquare().cartesianSquare().map { (a, b) ->
listOf(a.first, a.second, b.first, b.second)
}
return allSequences.maxOf { sequence -> priceChanges.sumOf { cost -> cost[sequence] ?: 0 } }
}

fun calculateNumber(secretNumber: Long): Long {
var result = secretNumber
result = prune(mix(result * 64, result))
result = prune(mix(result / 32, result))
return prune(mix(result * 2048, result))
}

fun mix(value: Long, secretNumber: Long): Long {
return value xor secretNumber
}

fun prune(secretNumber: Long): Long {
return secretNumber % 16777216L
}
}

fun main() {
Day22().execute()
}
1 change: 1 addition & 0 deletions src/main/kotlin/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ fun List<String>.chunkedInput(): List<List<String>> {
return this.joinToString("\n").split("\n\n").map { it.split("\n") }
}

fun <T> Iterable<T>.cartesianSquare(): List<Pair<T, T>> = flatMap { v -> map { v to it } }

0 comments on commit 8d1c806

Please sign in to comment.