diff --git a/src/aoc2024/Day22.kt b/src/aoc2024/Day22.kt index 37de268..7ef9cd2 100644 --- a/src/aoc2024/Day22.kt +++ b/src/aoc2024/Day22.kt @@ -36,7 +36,7 @@ fun main() { fun part1(input: List) = input.map(String::toInt).sumOf { priceSequence(it).elementAt(2000).toLong() } - fun part2(input: List): Int { + fun part2bruteforce(input: List): 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): Int { + val map = HashMap>() + 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()