-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 32 additions & 1 deletion
33
2024/kotlin/src/main/kotlin/com/tymscar/day09/part1/part1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
2024/kotlin/src/main/kotlin/com/tymscar/day09/part2/part2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |