Skip to content

Commit a0fd273

Browse files
authored
Use correct data structure (#264)
1 parent 09f52d5 commit a0fd273

File tree

1 file changed

+7
-7
lines changed
  • src/main/kotlin/me/peckb/aoc/_2024/calendar/day05

1 file changed

+7
-7
lines changed

src/main/kotlin/me/peckb/aoc/_2024/calendar/day05/Day05.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ class Day05 @Inject constructor(
2020
sumMiddlePages(invalidUpdates.map { it.reSort(rules) })
2121
}
2222

23-
private fun Sequence<String>.parseInput(): Pair<Map<Rule, Unit>, List<Update>> {
24-
val rules = mutableMapOf<Rule, Unit>()
23+
private fun Sequence<String>.parseInput(): Pair<Set<Rule>, List<Update>> {
24+
val rules = mutableSetOf<Rule>()
2525
val updates = mutableListOf<Update>()
2626

2727
var readingRules = true
2828
this.forEach { line ->
2929
if (line.isBlank()) { readingRules = false }
3030
else {
3131
if (readingRules) {
32-
rules[line.split("|").map { it.toInt() }.let{ (b, a) -> Rule(b, a) }] = Unit
32+
rules.add(line.split("|").map { it.toInt() }.let{ (b, a) -> Rule(b, a) })
3333
} else {
3434
updates.add(Update(line.split(",").map { it.toInt() }))
3535
}
@@ -39,10 +39,10 @@ class Day05 @Inject constructor(
3939
return rules to updates
4040
}
4141

42-
private fun validate(rules: Map<Rule, Unit>, updates: List<Update>): Pair<List<Update>, List<Update>> {
42+
private fun validate(rules: Set<Rule>, updates: List<Update>): Pair<List<Update>, List<Update>> {
4343
return updates.partition { update ->
4444
update.pages.windowed(2).all { (before, after) ->
45-
rules.containsKey(Rule(before, after))
45+
rules.contains(Rule(before, after))
4646
}
4747
}
4848
}
@@ -53,7 +53,7 @@ class Day05 @Inject constructor(
5353
data class Rule(val before: Int, val after: Int)
5454

5555
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
56+
fun reSort(rules: Set<Rule>) = pages.sortedWith { before, after ->
57+
if (rules.contains(Rule(before, after))) -1 else 1
5858
}.let(::Update)
5959
}

0 commit comments

Comments
 (0)