From 6bc6836314c1af78a455cbddcf36ef319ae7605e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Fri, 6 Dec 2024 00:24:26 +0100 Subject: [PATCH] 2024 Day 05 part 2 re-written to not sort twice. --- src/main/kotlin/no/rodland/advent_2024/Day05.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/no/rodland/advent_2024/Day05.kt b/src/main/kotlin/no/rodland/advent_2024/Day05.kt index 5618a7bc..aa4813c6 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day05.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day05.kt @@ -26,10 +26,13 @@ class Day05(val input: List) : Day>, Li } override fun partTwo(): Int { - return pages - .filterNot { it.valid() } - .map { it.sort() } - .sumOf { it[it.size / 2] } + // only sorting once + return pages.map { it.sort() }.zip(pages).filter { it.first != it.second }.map { it.first }.sumOf { it[it.size / 2] } + // more readable + // return pages + // .filterNot { it.valid() } + // .map { it.sort() } + // .sumOf { it[it.size / 2] } } private fun List.valid(): Boolean = sort() == this