Skip to content

Commit

Permalink
Finished 2024 day 9 both parts
Browse files Browse the repository at this point in the history
  • Loading branch information
tymscar committed Dec 9, 2024
1 parent 6bc3d50 commit e643858
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
9 changes: 9 additions & 0 deletions 2024/kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.tymscar

import com.tymscar.day01.solve as day01
import com.tymscar.day02.solve as day02
import com.tymscar.day03.solve as day03
import com.tymscar.day04.solve as day04
import com.tymscar.day05.solve as day05
import com.tymscar.day06.solve as day06
import com.tymscar.day07.solve as day07
import com.tymscar.day08.solve as day08
import com.tymscar.day09.solve as day09

fun main() {
day01()
Expand All @@ -11,4 +19,5 @@ fun main() {
day06()
day07()
day08()
day09()
}
33 changes: 32 additions & 1 deletion 2024/kotlin/src/main/kotlin/com/tymscar/day09/part1/part1.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
package com.tymscar.day09.part1

private sealed class Block {
data class File(val id: Long) : Block()
class Space : Block()
}

fun solve(input: String): String {
return input
val memory = input
.withIndex()
.fold((0L to listOf<Block>())) { (currId, currList), value ->
when (value.index % 2) {
0 -> (currId + 1 to (currList + List(value.value.digitToInt()) { Block.File(currId) }))
else -> (currId to (currList + List(value.value.digitToInt()) { Block.Space() }))
}
}.second

val fragmented = memory
.fold((0 to memory.count() - 1) to listOf<Block.File>()) { (indices, currList), value ->
var (indexLeft, indexRight) = indices
while (memory[indexRight] is Block.Space) indexRight--
if (indexLeft > indexRight) {
return@fold (indices to currList)
}

when (value) {
is Block.File -> ((indexLeft + 1 to indexRight) to (currList + memory[indexLeft] as Block.File))
is Block.Space -> ((indexLeft + 1 to indexRight - 1) to (currList + memory[indexRight] as Block.File))
}
}.second

return fragmented
.withIndex()
.sumOf { it.value.id * it.index }
.toString()
}
57 changes: 56 additions & 1 deletion 2024/kotlin/src/main/kotlin/com/tymscar/day09/part2/part2.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
package com.tymscar.day09.part2

private sealed class Block {
data class File(val id: Long) : Block()
class Space : Block()
}

private data class Chunk(val content: Block, val length: Int)

fun solve(input: String): String {
return input
val memory = input
.withIndex()
.fold((0L to listOf<Chunk>())) { (currId, currList), value ->
when (value.index % 2) {
0 -> (currId + 1 to (currList + Chunk(Block.File(currId), value.value.digitToInt())))
else -> (currId to (currList + Chunk(Block.Space(), value.value.digitToInt())))
}
}.second.toMutableList()

val chunksIdsToMove = memory
.mapNotNull {
when (it.content) {
is Block.File -> it.content.id
is Block.Space -> null
}
}.reversed()

chunksIdsToMove.forEach { chunkId ->
val chunk = memory.last { it.content is Block.File && (it.content).id == chunkId }
val chunkIndex = memory.indexOf(chunk)

val emptyChunk = memory.firstOrNull() { it.content is Block.Space && it.length >= chunk.length }
if (emptyChunk == null) return@forEach
val emptyChunkIndex = memory.indexOf(emptyChunk)

if (emptyChunkIndex >= chunkIndex) return@forEach
when {
chunk.length == emptyChunk.length -> {
memory[emptyChunkIndex] = chunk
memory[chunkIndex] = emptyChunk
}

chunk.length < emptyChunk.length -> {
memory[emptyChunkIndex] = Chunk(Block.File(chunkId), chunk.length)
memory[chunkIndex] = Chunk(Block.Space(), chunk.length)
memory.add(emptyChunkIndex + 1, Chunk(Block.Space(), emptyChunk.length - chunk.length))
}
}
}

return memory
.flatMap { List(it.length) { _ -> it.content } }
.withIndex()
.sumOf {
when (it.value) {
is Block.File -> (it.value as Block.File).id * it.index
is Block.Space -> 0
}
}.toString()
}

0 comments on commit e643858

Please sign in to comment.