Skip to content

Commit a95d20f

Browse files
committed
2024 Day 09 Part 2
1 parent 7d97b10 commit a95d20f

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

src/main/kotlin/de/jball/aoc2024/day09/Day09.kt

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,64 @@ package de.jball.aoc2024.day09
22

33
import de.jball.AdventOfCodeDay
44

5-
class Day09(test: Boolean = false): AdventOfCodeDay<Long>(test, 1928, 0) {
6-
val fileDb = input[0].windowed(2, 2, partialWindows = true).flatMapIndexed { index, blocks ->
7-
val fileSize = blocks.first().toString().toInt()
8-
val gapSize = if (blocks.length > 1) blocks.last().toString().toInt() else 0
9-
val file = List<Long?>(fileSize) { index.toLong() }
10-
val gap = List<Long?>(gapSize) { null }
11-
file + gap
12-
}.toMutableList()
5+
class Day09(test: Boolean = false): AdventOfCodeDay<Long>(test, 1928, 2858) {
6+
val fileDb = input[0].windowed(2, 2, partialWindows = true)
7+
.map { blocks ->
8+
val fileSize = blocks.first().toString().toInt()
9+
val gapSize = if (blocks.length > 1) blocks.last().toString().toInt() else 0
10+
Pair(fileSize, gapSize)
11+
}
1312

1413
override fun part1(): Long {
15-
var currentGap = fileDb.indexOfFirst { it == null }
16-
var lastValue = fileDb.indexOfLast { it != null }
14+
val defragmented = fileDb.flatMapIndexed { index, (fileSize, gapSize) ->
15+
val file = List<Long?>(fileSize) { index.toLong() }
16+
val gap = List<Long?>(gapSize) { null }
17+
file + gap
18+
}.toMutableList()
19+
20+
var currentGap = defragmented.indexOfFirst { it == null }
21+
var lastValue = defragmented.indexOfLast { it != null }
1722

1823
while(currentGap < lastValue) {
19-
fileDb[currentGap] = fileDb[lastValue]
20-
fileDb[lastValue] = null
21-
currentGap = fileDb.indexOfFirst { it == null }
22-
lastValue = fileDb.indexOfLast { it != null }
24+
defragmented[currentGap] = defragmented[lastValue]
25+
defragmented[lastValue] = null
26+
currentGap = defragmented.indexOfFirst { it == null }
27+
lastValue = defragmented.indexOfLast { it != null }
2328
}
24-
return fileDb.filter { it != null }
29+
return defragmented.filter { it != null }
2530
.mapIndexed { index, entry ->
2631
index.toLong() * entry!!
2732
}.sum()
2833
}
2934

3035
override fun part2(): Long {
31-
TODO("Not yet implemented")
36+
val defragmented = FileSystem(fileDb.mapIndexed { index, (fileSize, gapSize) ->
37+
FileWithGap(index, fileSize, gapSize)
38+
}.toMutableList())
39+
40+
for (currentFile in (fileDb.size - 1 downTo 0)) {
41+
var currentGap = 0
42+
val currentFilePos = defragmented.files.indexOfFirst { it.id == currentFile }
43+
val (_, currentFileSize, currentGapSize) = defragmented.files[currentFilePos]
44+
while (defragmented.files[currentGap].id != currentFile) {
45+
if (defragmented.files[currentGap].gap >= currentFileSize) {
46+
val (file, fileSize, gap) = defragmented.files[currentGap]
47+
defragmented.files[currentGap] = FileWithGap(file, fileSize, 0)
48+
defragmented.files.add(currentGap + 1, FileWithGap(currentFile, currentFileSize, gap-currentFileSize))
49+
defragmented.files.removeAt(currentFilePos+1)
50+
val (previousFile, previousFileSize, previousGap) = defragmented.files[currentFilePos]
51+
defragmented.files[currentFilePos] = FileWithGap(previousFile, previousFileSize, previousGap+currentFileSize+currentGapSize)
52+
break
53+
}
54+
currentGap++
55+
}
56+
}
57+
58+
val reduced = defragmented.files.fold(Pair(0L,0L)) { (index, acc), nextFile ->
59+
val value = (index until index + nextFile.size).sumOf { it * nextFile.id }
60+
Pair(index + nextFile.size + nextFile.gap, value + acc)
61+
}
62+
return reduced.second
3263
}
3364
}
3465

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.jball.aoc2024.day09
2+
3+
data class FileSystem(val files: MutableList<FileWithGap>) {
4+
override fun toString(): String {
5+
return files.joinToString("")
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.jball.aoc2024.day09
2+
3+
data class FileWithGap(val id: Int, val size: Int, val gap: Int) {
4+
override fun toString(): String {
5+
return List(size) { id.toString() }.joinToString("") + List(gap) { "." }.joinToString("")
6+
}
7+
}

src/test/kotlin/de/jball/aoc2024/Tests2024.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class Tests2024 {
5454
}
5555

5656
@Test
57-
@Ignore
5857
fun day09() {
5958
Day09(true).run()
6059
}

0 commit comments

Comments
 (0)