Skip to content

Commit

Permalink
[2024/5] Print Queue (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 5, 2024
1 parent 7d60244 commit ecaf035
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| 2021 |||||||||||| ||| | | | | | | | | | | | 26 |
| 2022 |||||||||||||| | | | | | | || | | || 28 |
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
| 2024 ||||| | | | | | | | | | | | | | | | | | | | | | 9 |
| 2024 ||||| | | | | | | | | | | | | | | | | | | | | | 10 |

## 🛷 How to run

Expand Down Expand Up @@ -168,7 +168,7 @@ e.g. `HandyHaversacks`)*
| | 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [[Code](src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day02RedNosedReportsSpec.kt)] | `572` | `612` |
| | 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [[Code](src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt)] | `174960292` | `56275602` |
| | 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [[Code](src/main/kotlin/adventofcode/year2024/Day04CeresSearch.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day04CeresSearchSpec.kt)] | `2483` | `1925` |
| | 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [[Code](src/main/kotlin/adventofcode/year2024/Day05PrintQueue.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day05PrintQueueSpec.kt)] | `6498` | |
| | 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [[Code](src/main/kotlin/adventofcode/year2024/Day05PrintQueue.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day05PrintQueueSpec.kt)] | `6498` | `5017` |

## 🕯️ Useful commands

Expand Down
20 changes: 11 additions & 9 deletions src/main/kotlin/adventofcode/year2024/Day05PrintQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class Day05PrintQueue(customInput: PuzzleInput? = null) : Puzzle(customInput) {

override fun partOne() =
updates
.mapNotNull { update ->
val filteredRules = rules.filter { rule -> update.containsAll(rule) }
.filter { update -> update.isOrdered(rules) }
.sumOf { update -> update[update.size / 2] }

if (filteredRules.containsAll(update.windowed(2))) {
update[update.size / 2]
} else {
null
}
}
.sum()
override fun partTwo() =
updates
.filterNot { update -> update.isOrdered(rules) }
.map { update -> update.sortedWith { a, b -> if (rules.contains(listOf(a, b))) -1 else 1 } }
.sumOf { update -> update[update.size / 2] }

companion object {
private fun List<Int>.isOrdered(rules: List<List<Int>>) = rules.containsAll(windowed(2))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package adventofcode.year2024

import adventofcode.PuzzleBaseSpec

class Day05PrintQueueSpec : PuzzleBaseSpec(143)
class Day05PrintQueueSpec : PuzzleBaseSpec(143, 123)

0 comments on commit ecaf035

Please sign in to comment.