Hi. Taking the limited input in mind, then recursion is a possibility in this case. The approach with sequences sounds great, but here it might be a bit over engineering. The calculating approach rocks! Great talk! My naive suggestion: fun getInput(filePath: String): List = java.io.File(ClassLoader.getSystemResource(filePath).file).readLines().map { it.split(" ").map(String::toInt) } fun List.extrapolateNext(): Int = if (this.all { it == 0 }) 0 else this.last() + this.zipWithNext { a, b -> b - a }.extrapolateNext() fun main() { println(getInput("Day09Input.txt").sumOf { it.extrapolateNext() }) println(getInput("Day09Input.txt").sumOf { it.reversed().extrapolateNext() }) }
Hi.
Taking the limited input in mind, then recursion is a possibility in this case. The approach with sequences sounds great, but here it might be a bit over engineering. The calculating approach rocks!
Great talk!
My naive suggestion:
fun getInput(filePath: String): List = java.io.File(ClassLoader.getSystemResource(filePath).file).readLines().map { it.split(" ").map(String::toInt) }
fun List.extrapolateNext(): Int = if (this.all { it == 0 }) 0 else this.last() + this.zipWithNext { a, b -> b - a }.extrapolateNext()
fun main() {
println(getInput("Day09Input.txt").sumOf { it.extrapolateNext() })
println(getInput("Day09Input.txt").sumOf { it.reversed().extrapolateNext() })
}