Skip to content

Commit

Permalink
2024 Day 05 Part2 - reversing list of elements to move - avoiding mul…
Browse files Browse the repository at this point in the history
…tiple move-passes.
  • Loading branch information
fmmr committed Dec 5, 2024
1 parent 40723fd commit b576d7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/main/kotlin/no/rodland/advent_2024/Day05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ class Day05(val input: List<String>) : Day<Int, Int, Pair<List<Pair<Int, Int>>,
.filterNot { it in rules }
.groupBy({ it.first }, { it.second })

val fold = breaking.keys.fold(this) { acc, b ->
acc.move(b, breaking[b]!!)
}
return if (fold.valid()){
// println("yeah. $this $fold $breaking")
fold
}else{
// still not right - just run it again
// println("reordering. $this $fold $breaking")
fold.reorder()
}
return breaking.keys.reversed().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.
// by starting at the end we don't have to.
// return if (fold.valid()) {
// println("yeah. $this $fold $breaking")
// } else {
// // still not right - just run it again
// println("reordering. $this $fold $breaking")
// fold.reorder()
// }
}

private fun List<Int>.move(element: Int, after: List<Int>): List<Int> {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/no/rodland/advent_2024/Day05Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class Day05Test {
{ Day05(data05) },
{ Day05(test05) },
numTestPart1 = 10,
numTestPart2 = 1,
numTestPart2 = 10,
)

@Nested
Expand Down

0 comments on commit b576d7f

Please sign in to comment.