Day 22 kotliniziation for more concise code.
This commit is contained in:
parent
343414cd04
commit
e222975b2a
@ -27,58 +27,43 @@ fun main() {
|
||||
2024
|
||||
"""
|
||||
|
||||
fun next(number: Int): Int {
|
||||
val r1 = (number xor (number shl 6)) and 0xff_ff_ff
|
||||
val r2 = (r1 xor (r1 shr 5)) and 0xff_ff_ff
|
||||
val r3 = (r2 xor (r2 shl 11)) and 0xff_ff_ff
|
||||
return r3
|
||||
fun priceSequence(seed: Int) = generateSequence(seed) {
|
||||
val r1 = (it xor (it shl 6)) and 0xff_ff_ff
|
||||
val r2 = r1 xor (r1 shr 5)
|
||||
(r2 xor (r2 shl 11)) and 0xff_ff_ff
|
||||
}
|
||||
|
||||
fun part1(input: List<String>): Long {
|
||||
var sum = 0L
|
||||
for (i in input.map(String::toInt)) {
|
||||
var v = i
|
||||
for (p in 1..2000) {
|
||||
v = next(v)
|
||||
}
|
||||
sum += v
|
||||
}
|
||||
return sum
|
||||
}
|
||||
fun part1(input: List<String>) =
|
||||
input.map(String::toInt).sumOf { priceSequence(it).elementAt(2000).toLong() }
|
||||
|
||||
fun part2(input: List<String>): Int {
|
||||
val normalStrings = ArrayList<String>(2500)
|
||||
val deltaStrings = ArrayList<String>(2500)
|
||||
for (i in input.map(String::toInt)) {
|
||||
val strings = input.map(String::toInt).map {
|
||||
val normalString = StringBuffer(2024)
|
||||
val deltaString = StringBuffer(2024)
|
||||
var v = i
|
||||
var lastd = v % 10
|
||||
for (p in 1..2000) {
|
||||
v = next(v)
|
||||
val d = v % 10
|
||||
var lastd = it % 10
|
||||
priceSequence(it).take(2001).forEach {
|
||||
val d = it % 10
|
||||
val delta = d - lastd
|
||||
normalString.append(d)
|
||||
deltaString.append('j' + delta)
|
||||
lastd = d
|
||||
}
|
||||
normalStrings.add(normalString.toString())
|
||||
deltaStrings.add(deltaString.toString())
|
||||
deltaString.toString() to normalString.toString()
|
||||
}
|
||||
|
||||
// it's not my fault that brute force is still working
|
||||
// note that there are less than 19^4 valid combinations, but we don't care
|
||||
return runBlocking {
|
||||
(0..(19 * 19 * 19 * 19)).map {
|
||||
async(Dispatchers.Default) {
|
||||
val tokenBuffer = StringBuffer(4)
|
||||
tokenBuffer.append('a' + (it / (19 * 19 * 19)))
|
||||
tokenBuffer.append('a' + ((it / (19 * 19)) % 19))
|
||||
tokenBuffer.append('a' + ((it / 19) % 19))
|
||||
tokenBuffer.append('a' + ((it % 19)))
|
||||
val token = tokenBuffer.toString()
|
||||
deltaStrings.indices.sumOf {
|
||||
val idx = deltaStrings[it].indexOf(token)
|
||||
if (idx >= 0) normalStrings[it][idx + 3].digitToInt() else 0
|
||||
val token = StringBuffer(4)
|
||||
.append('a' + (it / (19 * 19 * 19)))
|
||||
.append('a' + ((it / (19 * 19)) % 19))
|
||||
.append('a' + ((it / 19) % 19))
|
||||
.append('a' + ((it % 19))).toString()
|
||||
strings.sumOf {
|
||||
val idx = it.first.indexOf(token)
|
||||
if (idx >= 0) it.second[idx + 3].digitToInt() else 0
|
||||
}
|
||||
}
|
||||
}.awaitAll().max()
|
||||
|
Loading…
Reference in New Issue
Block a user