Skip to content

Commit

Permalink
2023 - Day09 - for part 2 - just reverse the strings in part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 9, 2023
1 parent cc9f6ef commit 423e835
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/kotlin/no/rodland/advent_2023/Day09.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ class Day09(val input: List<String>) : Day<Int, Int, List<List<Int>>> {
}

override fun partTwo(): Int {
return parsed.sumOf { it.previous() }
return parsed.map { it.reversed() }.sumOf { it.next() }
}

private fun List<Int>.next(): Int = if (all { it == 0 }) 0
else last() + diffs().next()
else last() + zipWithNext { a, b -> b - a }.next()

private fun List<Int>.previous(): Int = if (all { it == 0 }) 0
else first() - diffs().previous()

private fun List<Int>.diffs(): List<Int> = zipWithNext { a, b -> b - a }
// private fun List<Int>.previous(): Int = if (all { it == 0 }) 0
// else first() - diffs().previous()

override fun List<String>.parse(): List<List<Int>> {
return map { line ->
Expand Down

0 comments on commit 423e835

Please sign in to comment.