Skip to content

Commit bc4ecf0

Browse files
committed
Improve solution 2025-05 (Cafeteria)
Extract an extension function and an extension property to maintain a consistent abstraction level in the top-level functions.
1 parent 3d255f8 commit bc4ecf0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/main/kotlin/de/ronny_h/aoc/year2025/day05/Cafeteria.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@ class Cafeteria : AdventOfCode<Long>(2025, 5) {
1515
override fun part2(input: List<String>): Long =
1616
input.parseIngredients()
1717
.freshRanges
18-
.sortedBy { it.first }
19-
.fold(listOf<LongRange>()) { compactRanges, range ->
20-
compactRanges.mergeRange(range)
21-
}.sumOf {
22-
it.last - it.first + 1
23-
}
18+
.compact()
19+
.sumOf(LongRange::size)
2420
}
2521

2622
data class Ingredients(val freshRanges: List<LongRange>, val available: List<Long>)
2723

28-
// invariant: ranges in this MutableList are non-overlapping and sorted
24+
val LongRange.size: Long
25+
get() = last - first + 1
26+
27+
fun List<LongRange>.compact() =
28+
sortedBy { it.first }
29+
.fold(listOf<LongRange>()) { compactRanges, range ->
30+
compactRanges.mergeRange(range)
31+
}
32+
33+
// invariant: ranges in this List are non-overlapping and sorted
2934
// pre-condition: add operations only occur with ranges sorted by their first value
3035
fun List<LongRange>.mergeRange(toAdd: LongRange): List<LongRange> {
3136
if (none { toAdd.first in it || toAdd.last in it }) {
@@ -44,12 +49,12 @@ fun List<LongRange>.mergeRange(toAdd: LongRange): List<LongRange> {
4449
val lastOverlappingRangeIndex = indexOfLast { toAdd.last in it }
4550
if (toAdd.first in range && lastOverlappingRangeIndex == -1) {
4651
// the beginning overlaps with this range -> merge
47-
return this.take(i) + listOf(range.first..toAdd.last) + this.drop(i + 1)
52+
return take(i) + listOf(range.first..toAdd.last) + drop(i + 1)
4853
}
4954

5055
// overlaps with several ranges -> squash them
5156
val lastOverlappingRange = this[lastOverlappingRangeIndex]
52-
return this.take(i) + listOf(range.first..lastOverlappingRange.last) + this.drop(lastOverlappingRangeIndex + 1)
57+
return take(i) + listOf(range.first..lastOverlappingRange.last) + drop(lastOverlappingRangeIndex + 1)
5358
}
5459
error("If this line is reached, a case is missing")
5560
}

0 commit comments

Comments
 (0)