Skip to content

Commit 2321b65

Browse files
committed
Day 5
1 parent 926ab32 commit 2321b65

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/main/kotlin/y24/Day5.kt

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package y24
2+
3+
import common.puzzle.solvePuzzle
4+
import common.puzzle.Input
5+
import common.puzzle.Puzzle
6+
import common.datastructures.*
7+
import common.ext.*
8+
import common.puzzle.splitToInts
9+
import common.util.*
10+
import java.util.*
11+
import kotlin.math.*
12+
import kotlin.system.exitProcess
13+
14+
15+
fun main() = solvePuzzle(year = 2024, day = 5) { Day5(it) }
16+
17+
class Day5(val input: Input) : Puzzle {
18+
19+
data class PageRule(
20+
val before: Int,
21+
val after: Int,
22+
)
23+
24+
data class Update(
25+
val pageNumbers: List<Int>,
26+
) {
27+
val middle = pageNumbers[pageNumbers.size / 2]
28+
29+
fun satisfies(rules: List<PageRule>): Boolean {
30+
val pageIndices = pageNumbers.mapIndexed { index, i -> i to index }.toMap()
31+
return rules.all { (before, after) ->
32+
val beforeIndex = pageIndices[before] ?: return@all true
33+
val afterIndex = pageIndices[after] ?: return@all true
34+
beforeIndex < afterIndex
35+
}
36+
}
37+
38+
fun sortedBy(rules: List<PageRule>): Update {
39+
val sorted = pageNumbers.sortedWith(Comparator { p1, p2 ->
40+
val rule = rules.find { (it.before == p1 && it.after == p2) || (it.before == p2 && it.after == p1) }
41+
?: return@Comparator 0
42+
43+
if (rule.before == p1) -1 else 1
44+
})
45+
46+
return Update(sorted)
47+
}
48+
}
49+
50+
private val emptyLine = input.lines.indexOf("")
51+
private val rules = input.lines.subList(0, emptyLine).map { line ->
52+
val (before, after) = line.splitToInts("|")
53+
PageRule(before, after)
54+
}
55+
private val updates = input.lines.subList(emptyLine + 1, input.lines.size).map { line ->
56+
val pageNumbers = line.splitToInts(",")
57+
Update(pageNumbers)
58+
}
59+
60+
override fun solveLevel1(): Any {
61+
return updates.filter { it.satisfies(rules) }.sumOf { it.middle }
62+
}
63+
override fun solveLevel2(): Any {
64+
return updates.filter { !it.satisfies(rules) }.map { it.sortedBy(rules) }.sumOf { it.middle }
65+
}
66+
}

src/test/kotlin/y24/Day5Test.kt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package y24
2+
3+
import common.puzzle.Input
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Test
6+
7+
internal class Day5Test {
8+
private val sample = Input("""
9+
47|53
10+
97|13
11+
97|61
12+
97|47
13+
75|29
14+
61|13
15+
75|53
16+
29|13
17+
97|29
18+
53|29
19+
61|53
20+
97|53
21+
61|29
22+
47|13
23+
75|47
24+
97|75
25+
47|61
26+
75|61
27+
47|29
28+
75|13
29+
53|13
30+
31+
75,47,61,53,29
32+
97,61,53,29,13
33+
75,29,13
34+
75,97,47,61,53
35+
61,13,29
36+
97,13,75,29,47
37+
""".trimIndent())
38+
39+
private val day = Day5(sample)
40+
41+
@Test
42+
fun solveLevel1() {
43+
assertEquals(143, day.solveLevel1())
44+
}
45+
46+
@Test
47+
fun solveLevel2() {
48+
assertEquals(123, day.solveLevel2())
49+
}
50+
}

0 commit comments

Comments
 (0)