Skip to content

Commit

Permalink
2024 Day 05 Part2 - tweaked some comments on reversing.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 5, 2024
1 parent b576d7f commit 9060573
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/kotlin/no/rodland/advent_2024/Day05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ class Day05(val input: List<String>) : Day<Int, Int, Pair<List<Pair<Int, Int>>,
private fun List<Int>.reorder(): List<Int> {
val breaking = pairs()
.filterNot { it in rules }
.groupBy({ it.first }, { it.second })
.reversed() // see comment below **
.groupBy({ it.first }, { it.second }) // The returned map preserves the entry iteration order of the keys produced from the original collection.

return breaking.keys.reversed().fold(this) { acc, b -> acc.move(b, breaking[b]!!) }
return breaking.keys.fold(this) { acc, b -> acc.move(b, breaking[b]!!) }

// if not reversing the keys above - multiple passes must be done to ensure we have a right-ordered list.
// ** if not reversing the keys above - multiple passes must be done to ensure we have a right-ordered list.
// by starting at the end we don't have to.
// return if (fold.valid()) {
// println("yeah. $this $fold $breaking")
Expand Down

0 comments on commit 9060573

Please sign in to comment.