Revisited Day 22 for faster algorithm
This commit is contained in:
parent
277bc6cc8e
commit
0d5809398c
@ -36,7 +36,7 @@ fun main() {
|
||||
fun part1(input: List<String>) =
|
||||
input.map(String::toInt).sumOf { priceSequence(it).elementAt(2000).toLong() }
|
||||
|
||||
fun part2(input: List<String>): Int {
|
||||
fun part2bruteforce(input: List<String>): Int {
|
||||
val strings = input.map(String::toInt).map {
|
||||
val normalString = StringBuffer(2024)
|
||||
val deltaString = StringBuffer(2024)
|
||||
@ -70,6 +70,27 @@ fun main() {
|
||||
}
|
||||
}
|
||||
|
||||
fun part2(input: List<String>): Int {
|
||||
val map = HashMap<Int, MutableList<Int>>()
|
||||
input.map(String::toInt).forEach {
|
||||
var lastd = 0
|
||||
var code = 0
|
||||
var countDown = 4
|
||||
val seenArray = BooleanArray(19 * 19 * 19 * 19)
|
||||
for (v in priceSequence(it).take(2001)) {
|
||||
val d = v % 10
|
||||
val delta = d - lastd
|
||||
code = ((code * 19) % (19 * 19 * 19 * 19)) + (delta + 9)
|
||||
if (--countDown < 0 && !seenArray[code]) {
|
||||
map.getOrPut(code) { ArrayList() }.add(d)
|
||||
seenArray[code] = true
|
||||
}
|
||||
lastd = d
|
||||
}
|
||||
}
|
||||
return map.maxOf { it.value.sum() }
|
||||
}
|
||||
|
||||
// test if implementation meets criteria from the description, like:
|
||||
val testInput = inlineTestInput.trim().reader().readLines()
|
||||
val testInput2 = inlineTestInput2.trim().reader().readLines()
|
||||
|
Loading…
Reference in New Issue
Block a user