Skip to content

Commit

Permalink
2024 Day 05 part 2 re-written to not sort twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 5, 2024
1 parent 6c6da0b commit 6bc6836
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/kotlin/no/rodland/advent_2024/Day05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class Day05(val input: List<String>) : Day<Int, Int, Pair<Map<Int, Set<Int>>, 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<Int>.valid(): Boolean = sort() == this
Expand Down

0 comments on commit 6bc6836

Please sign in to comment.