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