Day 3: Lobby
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


Kotlin
I’m late to the party but I hope some of you will still be inspired by my submisison. This is an iterative solution. I began with a recursive solution that worked but I noticed that it should really be rewritten in an iterative way. The solution is also pointlessly optimized, to some degree, but that’s just what I like to do. 🙂
The logic follows a simple pattern of knowing which window of the battery bank to search in. Given the amount of batteries that remain to be turned on, if you were to turn on the last battery in the window, you’d need to turn on all the remaining batteries. So the window begins at one position past the prior battery and ends at the last battery you actually can choose to turn on. Once that has been turned on, all remaining ones need to be turned on. The window can only actually shrink to at least one position.
Code inside
class Day03 : AOCSolution { override val year = 2025 override val day = 3 override fun part1(inputFile: String): String { return readResourceBinary(inputFile).lineSequence().sumOf { batteryBank -> findHighestJoltage(batteryBank, 2) }.toString() } override fun part2(inputFile: String): String { return readResourceBinary(inputFile).lineSequence().sumOf { batteryBank -> findHighestJoltage(batteryBank, 12) }.toString() } private fun findHighestJoltage( bank: EightBitString, batteries: Int, ): Long { val digitsArray = ByteArray(batteries) { -1 } var lastDigitIndex = 0 repeat(batteries) { currentDigit -> val remainingDigits = batteries - currentDigit val lastIndex = bank.length - remainingDigits + 1 val maxIndex = bank.indexOfMax(lastDigitIndex, lastIndex) lastDigitIndex = maxIndex + 1 digitsArray[batteries - remainingDigits] = bank[maxIndex].toDigit() } return digitsArray.fold(0L) { acc, i -> acc * 10L + i } } private companion object { private fun ByteArray.lineSequence(): Sequence<EightBitString> { val buffer = EightBitString(this) var currentOffset = 0 return generateSequence { for (characterIndex in currentOffset until buffer.limit()) { if (buffer[characterIndex] == '\n') { val slice = buffer.subSequence(currentOffset, characterIndex) // Despite believing that `currentIndex` is not read, // it is indeed read the next time this generator is called. @Suppress("AssignedValueIsNeverRead") currentOffset = characterIndex + 1 return@generateSequence slice } } // A '\n' is always found, because the files end with a new line. return@generateSequence null } } private fun EightBitString.indexOfMax( startIndex: Int, endIndex: Int, ): Int { if (startIndex >= endIndex) { return -1 } var maxIndex = startIndex var max = 0.toByte() for (i in startIndex until endIndex) { val c = getByte(i) if (c > max) { maxIndex = i max = c } } return maxIndex } private fun Char.toDigit(): Byte = (this - '0').toByte() } }Its not a race, its a journey! Keep it up!