advent-of-code/2019/01/day1.kt
2019-12-02 09:57:13 +01:00

24 lines
569 B
Kotlin

package moe.kageru.aoc
fun main() {
val input = generateSequence(::readLine)
.map { it.toLong() }
.toList();
println("Part 1: ${part1(input)}")
println("Part 2: ${part2(input)}")
}
fun part1(numbers: List<Long>): Long = numbers.map(::cost).sum()
fun part2(numbers: List<Long>): Long = numbers.map { costRec(it, 0) }.sum()
fun cost(mass: Long): Long = (mass / 3) - 2
tailrec fun costRec(mass: Long, acc: Long): Long {
val cost = cost(mass)
return when {
cost <= 0 -> acc
else -> costRec(cost, acc + cost)
}
}