Advent of Code 2023 in Kotlin Day 9

Поделиться
HTML-код
  • Опубликовано: 2 дек 2024

Комментарии • 1

  • @JonHagemo
    @JonHagemo 11 месяцев назад

    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() })
    }