Skip to content

Commit 09f52d5

Browse files
authored
Day 05 2024 (#263)
1 parent b68d37d commit 09f52d5

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package me.peckb.aoc._2024.calendar.day05
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
6+
class Day05 @Inject constructor(
7+
private val generatorFactory: InputGeneratorFactory,
8+
) {
9+
fun partOne(filename: String) = generatorFactory.forFile(filename).read { input ->
10+
val (rules, updates) = input.parseInput()
11+
val (validUpdates, _) = validate(rules, updates)
12+
13+
sumMiddlePages(validUpdates)
14+
}
15+
16+
fun partTwo(filename: String) = generatorFactory.forFile(filename).read { input ->
17+
val (rules, updates) = input.parseInput()
18+
val (_, invalidUpdates) = validate(rules, updates)
19+
20+
sumMiddlePages(invalidUpdates.map { it.reSort(rules) })
21+
}
22+
23+
private fun Sequence<String>.parseInput(): Pair<Map<Rule, Unit>, List<Update>> {
24+
val rules = mutableMapOf<Rule, Unit>()
25+
val updates = mutableListOf<Update>()
26+
27+
var readingRules = true
28+
this.forEach { line ->
29+
if (line.isBlank()) { readingRules = false }
30+
else {
31+
if (readingRules) {
32+
rules[line.split("|").map { it.toInt() }.let{ (b, a) -> Rule(b, a) }] = Unit
33+
} else {
34+
updates.add(Update(line.split(",").map { it.toInt() }))
35+
}
36+
}
37+
}
38+
39+
return rules to updates
40+
}
41+
42+
private fun validate(rules: Map<Rule, Unit>, updates: List<Update>): Pair<List<Update>, List<Update>> {
43+
return updates.partition { update ->
44+
update.pages.windowed(2).all { (before, after) ->
45+
rules.containsKey(Rule(before, after))
46+
}
47+
}
48+
}
49+
50+
private fun sumMiddlePages(updates: List<Update>) = updates.sumOf { it.pages[(it.pages.size / 2)] }
51+
}
52+
53+
data class Rule(val before: Int, val after: Int)
54+
55+
data class Update(val pages: List<Int>) {
56+
fun reSort(rules: Map<Rule, Unit>) = pages.sortedWith { before, after ->
57+
if (rules.containsKey(Rule(before, after))) -1 else 1
58+
}.let(::Update)
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 5: Print Queue](https://adventofcode.com/2024/day/5)

src/test/kotlin/me/peckb/aoc/_2024/TestDayComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dagger.Component
44
import me.peckb.aoc._2024.calendar.day02.Day02Test
55
import me.peckb.aoc._2024.calendar.day03.Day03Test
66
import me.peckb.aoc._2024.calendar.day04.Day04Test
7+
import me.peckb.aoc._2024.calendar.day05.Day05Test
78
import javax.inject.Singleton
89
import me.peckb.aoc.DayComponent
910
import me.peckb.aoc.InputModule
@@ -16,4 +17,5 @@ internal interface TestDayComponent : DayComponent {
1617
fun inject(day02Test: Day02Test)
1718
fun inject(day03Test: Day03Test)
1819
fun inject(day04Test: Day04Test)
20+
fun inject(day05Test: Day05Test)
1921
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.peckb.aoc._2024.calendar.day05
2+
3+
import javax.inject.Inject
4+
5+
import me.peckb.aoc._2024.DaggerTestDayComponent
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.BeforeEach
8+
import org.junit.jupiter.api.Test
9+
10+
internal class Day05Test {
11+
@Inject
12+
lateinit var day05: Day05
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay05PartOne() {
21+
assertEquals(5129, day05.partOne(DAY_05))
22+
}
23+
24+
@Test
25+
fun testDay05PartTwo() {
26+
assertEquals(4077, day05.partTwo(DAY_05))
27+
}
28+
29+
companion object {
30+
private const val DAY_05: String = "advent-of-code-input/2024/day05.input"
31+
}
32+
}

0 commit comments

Comments
 (0)