Skip to content

Commit

Permalink
Add Day9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
derNiklaas committed Dec 9, 2024
1 parent 75133fd commit 89fdfb9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/kotlin/Day09.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import utils.AoCDay
import utils.splitAndMapToInt

class Day09 : AoCDay() {
val lengths = input.first().splitAndMapToInt("")
val writes = mutableMapOf<Int, Int>()
val spaces = mutableMapOf<Int, Int>()

init {
var id = 0
lengths.forEachIndexed { index, length ->
if (index % 2 == 0) {
writes += id to length
id++
} else {
spaces += (id) to length
}
}
}

// 2333133121414131402
// 2 3 1 3 2 4 4 3 4 2

override fun part1(): Any {
var writeState = true
var result = 0L

var spaces = this.spaces.toMutableMap()
var writes = this.writes.toMutableMap()

var totalIndex = 0

while (writes.isNotEmpty()) {
if (writeState) {
val (id, amount) = writes.entries.first()
writes.remove(id, amount)
repeat(amount) {
result += id * totalIndex.toLong()
totalIndex++
}
writeState = false
} else {
if (spaces.isEmpty()) {
writeState = true
continue
}
val (spaceId, amountOfSpaces) = spaces.entries.first()
spaces.remove(spaceId, amountOfSpaces)

repeat(amountOfSpaces) {
val (id, amountOfWrites) = writes.entries.last()
writes.remove(id, amountOfWrites)
if (amountOfWrites != 1) {
writes += id to amountOfWrites - 1
}
result += id * totalIndex
totalIndex++
}
writeState = true
}
}

return result
}

override fun part2(): Any {
return -1
}
}

fun main() {
Day09().execute()
}

0 comments on commit 89fdfb9

Please sign in to comment.