Advent of Code 2023 day 3
12/03/2025, 5:00 AMDan Fingal-Surma
12/03/2025, 5:12 AMDan Fingal-Surma
12/03/2025, 5:13 AMDan Fingal-Surma
12/03/2025, 5:15 AMMarcin Wisniowski
12/03/2025, 5:20 AMMarcin Wisniowski
12/03/2025, 5:20 AMMarcin Wisniowski
12/03/2025, 5:20 AMNeil Banman
12/03/2025, 5:25 AMy9san9
12/03/2025, 5:32 AMbj0
12/03/2025, 5:35 AMbj0
12/03/2025, 5:35 AMbj0
12/03/2025, 5:36 AMNeil Banman
12/03/2025, 6:06 AMNeil Banman
12/03/2025, 6:16 AMy9san9
12/03/2025, 6:36 AMPaul Woitaschek
12/03/2025, 7:04 AMAndriy
12/03/2025, 7:09 AMtailrec in prod 😝
class Day03(input: String) {
private val banks = input.lines()
fun solvePart1() = banks.sumOf {
findMaxJoltage(it, 2).toInt()
}
fun solvePart2() = banks.sumOf {
findMaxJoltage(it, 12).toLong()
}
tailrec fun findMaxJoltage(bank: String, count: Int, acc: String = ""): String =
if (count == 1) {
acc + bank.max()
} else {
val window = bank.dropLast(count - 1)
val maxDigit = window.withIndex().maxBy { it.value }
findMaxJoltage(bank.drop(maxDigit.index + 1), count - 1, acc + maxDigit.value)
}
}ephemient
12/03/2025, 7:10 AMAndriy
12/03/2025, 7:24 AMdata class Joltage actually as value class?y9san9
12/03/2025, 7:26 AMephemient
12/03/2025, 7:28 AMphldavies
12/03/2025, 7:44 AMDan Fingal-Surma
12/03/2025, 7:56 AMO(sqrt(n)) time on problem 2 though 😂Anirudh
12/03/2025, 8:01 AMKarloti
12/03/2025, 8:06 AMfun findMaxVoltageForBank(bank: String, target: Int): Long {
val bankLength = bank.length
val stack = mutableListOf<Char>()
bank.forEachIndexed { i, c ->
while (stack.isNotEmpty() && c > stack.last() && (stack.lastIndex + bankLength - i) >= target) stack.removeLast()
if (stack.size < target) stack.add(c)
}
return stack.joinToString("").toLong()
}
https://github.com/karloti/aoc-2025-in-kotlin/blob/main/src/Day03.ktMichael Böiers
12/03/2025, 8:08 AMfun main() {
val banks = generateSequence(::readLine).map { it.map(Char::digitToInt) }.toList()
fun List<Int>.solve(n: Int): Long {
var solution = listOf<Int>()
fun List<Int>.pick(n: Int, pos: Int = 0, picked: List<Int> = listOf()): List<Int>? {
if (n == 0) return picked
val candidates = pos..indices.last - (n - 1)
val max = candidates.maxOf { this[it] }
if (picked.size in solution.indices && solution[picked.size] >= max) return null
return candidates
.filter { this[it] == max }
.mapNotNull { pick(n - 1, it + 1, picked + max) }
.maxByOrNull { it.joinToString("").toLong() }
?.also { solution = it }
}
pick(n)
return solution.joinToString("").toLong()
}
listOf(2, 12).map { n -> banks.sumOf { it.solve(n) } }.forEach(::println)
}Michael Böiers
12/03/2025, 8:33 AMKai Yuan
12/03/2025, 9:04 AMMax Thiele
12/03/2025, 9:25 AMkingsley
12/03/2025, 9:54 AMMohammad Jaber
12/03/2025, 11:19 AMJaap Beetstra
12/03/2025, 11:28 AMcached (one argument) and cached2 (two arguments) memoization helper functions:
data class Bank(val digits: List<Int>) {
constructor(s: String) : this(s.toCharArray().map { it.digitToInt() })
fun maxOutput(outputSize: Int): Long = maxOutput(outputSize, digits.size)
val maxOutput = cached2 { outputSize: Int, inputSize: Int ->
when {
outputSize > inputSize -> -1L
outputSize == 0 -> 0L
else -> (0..<inputSize).maxOf { idx -> this(outputSize-1, idx)*10 + digits[idx] }
}
}
}T0liver
12/03/2025, 12:59 PMList<List<Int>> for reading from file (just like as right before me @Jaap Beetstra as I guess) and it's pretty effective (3.06ms for 12 digits and 2.42 for 2)
fun firstMaxJoltageN(bank: List<Int>, start: Int = 0, end: Int = 0): Int {
var index = start
for (i in start..<bank.size - end) {
if (bank[i] > bank[index]) {
index = i
if (bank[i] == 9) {
break
}
}
}
return index
}
fun determineMaxJoltageN(bank: List<Int>, n: Int): Long {
var maxJoltage = 0L
var id = 0
for (i in 0 until n) {
val index = firstMaxJoltageN(bank, id, n - i - 1)
maxJoltage = maxJoltage * 10 + bank[index]
id = index + 1
}
return maxJoltage
}phldavies
12/03/2025, 1:52 PMyear 2025 day 3 part 1
Scanning took 26.501us 👑: 17142
Tailrec took 30.153us (1.14x): 17142
Default took 39.516us (1.49x): 17142
year 2025 day 3 part 2
Scanning took 49.278us 👑: 169935154100102
Tailrec took 77.457us (1.57x): 169935154100102
Default took 89.909us (1.82x): 169935154100102Neil Banman
12/03/2025, 3:47 PMbut I like my new@Jaap Beetstra where is the hashmap stored? A few years back someone had a memo helper that used a CacheSupport Object that used some fancy casting to provide hashmap services on demand. I asked for permission to use it but then never did because it was slower than rolling my own, perhaps because it used a ConcurrentHashMap.(one argument) andcached(two arguments) memoization helper functionscached2
Jaap Beetstra
12/03/2025, 3:50 PMJaap Beetstra
12/03/2025, 3:55 PMinvoke operator. You can check my GitHub but the main logic is:
fun <K1, K2, V> cached2(block2: Cache2<K1, K2, V>.(K1, K2) -> V) = Cache2(block2)
abstract class Cache<K, V>(val items: MutableMap<K, V> = mutableMapOf()) {
operator fun get(key: K): V = items.getOrPut(key) { calculate(key) }
abstract fun calculate(key: K): V
}
class Cache2<K1, K2, V>(private val calculate2: Cache2<K1, K2, V>.(K1, K2) -> V) : Cache<Pair<K1, K2>, V>() {
operator fun invoke(key1: K1, key2: K2): V = get(key1 to key2)
override fun calculate(key: Pair<K1, K2>): V = calculate2(key.first, key.second)
}Neil Banman
12/03/2025, 4:02 PM