ok that was… wow (spoilers)
# advent-of-code
a
ok that was… wow (spoilers)
me naively thinking my program will finish running in the next thousand years without caching number values
Copy code
class Aoc2020Day10 : Problem(2020, 10) {
    private val jolts = lines.map { it.toLong() }.sorted()

    override fun solvePart1(): Any {
        val joltsLeft = jolts.toMutableList().apply { this += jolts.last() + 3 }

        var currentJolt = 0L
        val distributions = mutableMapOf(*(1..3).map { it to 0 }.toTypedArray())
        while (joltsLeft.size >= 1) {
            joltsLeft.first { it > currentJolt && it - currentJolt <= 3 }.let { newJolt ->
                val difference = (newJolt - currentJolt).toInt()
                distributions.replace(difference, distributions[difference]!! + 1)
                currentJolt = newJolt
                joltsLeft.remove(newJolt)
            }
        }

        return distributions.getValue(1) * distributions.getValue(3)
    }

    override fun solvePart2(): Any {
        val joltToCombinationsMapping = mutableMapOf<Long, Long>()
        
        fun countJoltCombos(curr: Long, joltsLeft: List<Long>): Long {
            return when {
                joltToCombinationsMapping.containsKey(curr) -> joltToCombinationsMapping.getValue(curr)
                joltsLeft.none { it - curr in 1..3 } || joltsLeft.size == 1 && joltsLeft.first() == jolts.maxOrNull()!! -> 1L
                else -> {
                    joltsLeft.filter { it - curr in 1..3 }
                        .sumOf { v -> countJoltCombos(v, joltsLeft.filter { num -> num > v }).apply { joltToCombinationsMapping[v] = this } }
                }
            }
        }
        return countJoltCombos(0, lines.map { it.toLong() })
    }
}