Skip to content

Commit abe3a3c

Browse files
committed
Day 09 cleanup.
1 parent 31c86f8 commit abe3a3c

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/main/kotlin/day09/day09.kt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
// Advent of Code 2024, Day 09.
22
// By Sebastian Raaphorst, 2024.
33

4+
// This code was a misery to write and could stand for a lot of improvement.
5+
46
package day09
57

68
import common.aocreader.fetchAdventOfCodeInput
79
import java.math.BigInteger
810

9-
typealias Range = LongRange
10-
typealias RangeList = MutableList<Range>
11+
private typealias Range = LongRange
12+
private typealias RangeList = MutableList<Range>
1113

1214
private fun Range.size(): Long =
1315
last - first + 1
1416

15-
data class File(val id: Int, var blocks: RangeList)
17+
private data class File(val id: Int, var blocks: RangeList)
1618

17-
class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
19+
private class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
1820
init {
1921
sortGaps()
2022
sortFiles()
@@ -36,7 +38,7 @@ class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
3638
}
3739

3840
/**
39-
* Reparse before using this.
41+
* Reparse before using this: solves part 1.
4042
*/
4143
fun rearrange1() {
4244
// We want to get the highest block and move it to the earliest position if
@@ -79,7 +81,7 @@ class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
7981
gaps += lastBlock
8082
}
8183

82-
gapSize < blockSize -> {
84+
else -> {
8385
// If the block is bigger than the gap, we divide the block in two
8486
// and move the last part to the gap.
8587
val dividingPoint = lastBlock.last - gapSize + 1
@@ -92,22 +94,23 @@ class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
9294

9395
gaps -= firstGap
9496

95-
// Do we need this?
97+
// We shouldn't need this.
9698
gaps += subBlock2
9799
}
98100
}
99101

100102
// Re-sort.
101103
sortFile(lastFile)
102-
mergeRanges(lastFile.blocks).let { lastFile.blocks = it }
104+
sortAndMergeRanges(lastFile.blocks).let { lastFile.blocks = it }
103105
sortFiles()
104-
sortGaps()
105-
gaps = mergeRanges(gaps)
106+
107+
// Sort and merge the gaps.
108+
gaps = sortAndMergeRanges(gaps)
106109
}
107110
}
108111

109112
/**
110-
* Reparse before using this.
113+
* Reparse before using this: solves part 2.
111114
*/
112115
fun rearrange2() {
113116
for (file in files.reversed()) {
@@ -137,13 +140,12 @@ class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
137140
}
138141
}
139142

140-
// Sort the gaps.
141-
sortGaps()
142-
gaps = mergeRanges(gaps)
143+
// Sort and merge the gaps.
144+
gaps = sortAndMergeRanges(gaps)
143145
}
144146
}
145147

146-
fun mergeRanges(ranges: MutableList<Range>): MutableList<Range> {
148+
fun sortAndMergeRanges(ranges: MutableList<Range>): MutableList<Range> {
147149
if (ranges.isEmpty()) return mutableListOf()
148150

149151
// Sort the ranges by their start values
@@ -154,18 +156,17 @@ class Filesystem(var files: MutableList<File>, var gaps: RangeList) {
154156

155157
for (range in sortedRanges.drop(1)) {
156158
if (range.first <= currentRange.last + 1) {
157-
// Merge ranges if they overlap or are contiguous
159+
// Merge ranges if they overlap or are contiguous.
158160
currentRange = currentRange.first..maxOf(currentRange.last, range.last)
159161
} else {
160-
// Add the non-overlapping range to the result
162+
// Add the non-overlapping range to the result.
161163
merged.add(currentRange)
162164
currentRange = range
163165
}
164166
}
165167

166168
// Add the final range
167169
merged.add(currentRange)
168-
169170
return merged
170171
}
171172

0 commit comments

Comments
 (0)