Complete day09
All checks were successful
Solve / example-action (push) Successful in 2m55s

This commit is contained in:
Logiar 2024-12-11 11:10:10 +01:00
parent 68eec5b3b3
commit 874622fc78
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
2 changed files with 109 additions and 1 deletions

108
day09.kts
View File

@ -6,5 +6,111 @@ val scanner = Scanner(System.`in`)
if (!scanner.hasNext()) { if (!scanner.hasNext()) {
throw RuntimeException("No input given.") throw RuntimeException("No input given.")
} }
val fileSystem = scanner.nextLine().split("").filter { it.isNotEmpty() }.map { it.toInt() }.toMutableList() val fileSystem = mutableListOf<Int>()
val fileSystemDesc = scanner.nextLine().split("").filter { it.isNotEmpty() }.map { it.toInt() }.toMutableList()
for (i in 0..fileSystemDesc.lastIndex) {
for (j in 0 until fileSystemDesc[i]) {
if ((i and 1) == 0) {
fileSystem.add(i / 2)
} else {
fileSystem.add(-1)
}
}
}
val compressed = compressPartOne(fileSystem)
val checksumOne = calculateChecksum(compressed)
println("Part one checksum: $checksumOne")
val compressedTwo = compressPartTwo(fileSystem)
val checksumTwo = calculateChecksum(compressedTwo)
println("Part two checksum: $checksumTwo")
fun compressPartOne(fileSystem: MutableList<Int>): MutableList<Int> {
val compressed = fileSystem.toMutableList()
var freeSpacePointer = 0
var lastSectorPointer = compressed.lastIndex
while (freeSpacePointer <= lastSectorPointer) {
if (compressed[freeSpacePointer] != -1) {
freeSpacePointer++
continue
}
if (compressed[lastSectorPointer] == -1) {
lastSectorPointer--
continue
}
val tmp = compressed[freeSpacePointer]
compressed[freeSpacePointer] = compressed[lastSectorPointer]
compressed[lastSectorPointer] = tmp
lastSectorPointer--
freeSpacePointer++
}
return compressed
}
fun calculateChecksum(compressed: MutableList<Int>): Long {
var checksum = 0L
for (i in 0..compressed.lastIndex) {
if (compressed[i] != -1) {
checksum += i.toLong() * compressed[i].toLong()
}
}
return checksum
}
fun compressPartTwo(fileSystem: MutableList<Int>): MutableList<Int> {
val compressed = fileSystem.toMutableList()
var relevantFileId = compressed[compressed.lastIndex]
for (i in compressed.lastIndex downTo 0) {
if (compressed[i] == relevantFileId && relevantFileId >= 0) {
val fileSize = findFileSize(compressed, i, relevantFileId)
val startIndex = findStartIndexOfEmptySpaceOfLength(compressed, fileSize)
if (startIndex != -1 && startIndex < i) {
for (j in 0 until fileSize) {
compressed[startIndex + j] = compressed[i - j]
compressed[i - j] = -1
}
}
relevantFileId--
}
}
return compressed
}
fun findFileSize(compressed: MutableList<Int>, i: Int, relevantFileId: Int): Int {
var fileSize = 0
var j = i
while (j >= 0 && compressed[j] == relevantFileId) {
fileSize++
j--
}
return fileSize
}
fun findStartIndexOfEmptySpaceOfLength(compressed: MutableList<Int>, fileSize: Int): Int {
var startIndex = 0
while (startIndex < compressed.size) {
if (compressed[startIndex] != -1) {
startIndex++
} else {
var emptySpaceLength = 0
for (i in startIndex until (startIndex + fileSize)) {
if (i >= compressed.size) {
return -1
}
if (compressed[i] != -1) {
break
} else {
emptySpaceLength++
}
if (emptySpaceLength == fileSize) {
return startIndex
}
}
startIndex += emptySpaceLength
}
}
return -1
}

2
day09.kts.testvalue Normal file
View File

@ -0,0 +1,2 @@
Part one checksum: 6607511583593
Part two checksum: 6636608781232