Skip to content

Commit

Permalink
Day 5: Print Queue (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
andilau authored Dec 7, 2024
1 parent ac91f77 commit 01361ea
Show file tree
Hide file tree
Showing 6 changed files with 1,487 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Advent of Code is an Advent calendar of small programming puzzles by [Eric Wastl
- Day 2: [Red-Nosed Reports](https://adventofcode.com/2024/day/2) -- [Day2.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day2.kt)
- Day 3: [Mull It Over](https://adventofcode.com/2024/day/3) -- [Day3.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day3.kt)
- Day 4: [Ceres Search](https://adventofcode.com/2024/day/4) -- [Day4.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day4.kt)
- Day 5: [Print Queue](https://adventofcode.com/2024/day/5) -- [Day4.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day5.kt)

### Features

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'advent-of-code-starter'
rootProject.name = 'advent-of-code-2024'
47 changes: 47 additions & 0 deletions src/main/kotlin/days/Day5.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package days

@AdventOfCodePuzzle(
name = "Print Queue",
url = "https://adventofcode.com/2024/day/5",
date = Date(day = 5, year = 2024)
)
class Day5(input: List<String>) : Puzzle {

private val pageOrderingRules = input
.takeWhile { it.isNotEmpty() }
.map { line -> line.split('|').let { it[0].toInt() to it[1].toInt() } }
private val pageToProduce = input
.dropWhile { it.isNotEmpty() }
.drop(1)
.map { it.split(',').map { it.toInt() } }

override fun partOne(): Int = pageToProduce
.filter { pages -> validOrder(pages) }
.sumOf(::middlePage)

override fun partTwo() = pageToProduce
.filterNot { pages -> validOrder(pages) }
.map { pages -> reorder(pages) }
.sumOf { middlePage(it) }

private fun reorder(pages: List<Int>): List<Int> {
val selectedRules = pageOrderingRules.filter { (prev, next) -> prev in pages && next in pages }
val eachCount = selectedRules.groupingBy { it.first }.eachCount().toMutableMap()
selectedRules.map { it.second }.forEach { eachCount.putIfAbsent(it, 0) }

return eachCount
.map { (page, degree) -> degree to page }
.toMap()
.toSortedMap(Comparator.naturalOrder<Int?>().reversed())
.values.toList()
}

private fun validOrder(pages: List<Int>): Boolean {
return pageOrderingRules
.filter { (prev, next) -> prev in pages && next in pages }
.all { (prev, next) -> pages.indexOf(prev) < pages.indexOf(next) }
}

private fun middlePage(it: List<Int>) = it[it.size / 2]

}
Loading

0 comments on commit 01361ea

Please sign in to comment.