From 423e835f33be98e87a83b494484730ba4f6762f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Sun, 10 Dec 2023 00:16:09 +0100 Subject: [PATCH] 2023 - Day09 - for part 2 - just reverse the strings in part 1 --- src/main/kotlin/no/rodland/advent_2023/Day09.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/no/rodland/advent_2023/Day09.kt b/src/main/kotlin/no/rodland/advent_2023/Day09.kt index eab605ac..73432699 100644 --- a/src/main/kotlin/no/rodland/advent_2023/Day09.kt +++ b/src/main/kotlin/no/rodland/advent_2023/Day09.kt @@ -14,16 +14,14 @@ class Day09(val input: List) : Day>> { } override fun partTwo(): Int { - return parsed.sumOf { it.previous() } + return parsed.map { it.reversed() }.sumOf { it.next() } } private fun List.next(): Int = if (all { it == 0 }) 0 - else last() + diffs().next() + else last() + zipWithNext { a, b -> b - a }.next() - private fun List.previous(): Int = if (all { it == 0 }) 0 - else first() - diffs().previous() - - private fun List.diffs(): List = zipWithNext { a, b -> b - a } +// private fun List.previous(): Int = if (all { it == 0 }) 0 +// else first() - diffs().previous() override fun List.parse(): List> { return map { line ->