part 1
65.0 µs
counttop 3
rewrites
v1 1.2xv2 fastestv3 6.1x
warm-up 3.7x faster once warm
215 KB allocated per run
kotlin
v2 is the fastest
part 1
65.0 µs
counttop 3
rewrites
warm-up 3.7x faster once warm
215 KB allocated per run
part 2
63.9 µs
counttop 3
rewrites
warm-up 5.3x faster once warm
215 KB allocated per run
package day01.v2
import aoc.section
import aoc.solve
const val SIZE = 3
inline fun solve(input: String, start: Int, compare: (Int, Int) -> Boolean): Int {
val counts = section("count") {
val counts = mutableMapOf<Int, Int>()
for (number in input.lineSequence().map { it.toInt() }) {
counts.compute(number) { _, v -> if (v == null) 1 else v + 1 }
}
counts
}
val targets = IntArray(SIZE) { -1 }
val targetCounts = IntArray(SIZE) { start }
section("top $SIZE") {
for ((key, value) in counts) {
for (i in 0 until SIZE) {
if (!compare(value, targetCounts[i])) break
if (i > 0) {
targets[i - 1] = targets[i]
targetCounts[i - 1] = targetCounts[i]
}
targets[i] = key
targetCounts[i] = value
}
}
}
return targets.reduce { acc, i -> acc * i }
}
fun part1(input: String): Any? {
return solve(input, -1) { a, b -> a > b }
}
fun part2(input: String): Any? {
return solve(input, 1000) { a, b -> a < b }
}
fun main() = solve(1, version = 2)