Skip to content

Commit

Permalink
Add Day11
Browse files Browse the repository at this point in the history
  • Loading branch information
derNiklaas committed Dec 11, 2024
1 parent 8cd5c73 commit 00f6e43
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/kotlin/Day11.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import utils.AoCDay
import utils.isEven
import utils.splitAndMapToLong

class Day11 : AoCDay() {
val stones = input.first().splitAndMapToLong()

override fun part1(): Any {
return blink(25)
}

override fun part2(): Any {
return blink(75)
}

private fun blink(times: Int): Long {
var map = mutableMapOf<Long, Long>()
stones.forEach {
map[it] = (map[it] ?: 0) + 1
}
repeat(times) {
var newMap = mutableMapOf<Long, Long>()
for ((number, amount) in map) {
if (number == 0L) {
newMap[1] = (newMap[1] ?: 0) + amount
} else if (number.toString().length.isEven()) {
val string = number.toString()
val half = string.length / 2
val firstHalf = string.substring(0, half).toLong()
val secondHalf = string.substring(half).toLong()
newMap[firstHalf] = (newMap[firstHalf] ?: 0) + amount
newMap[secondHalf] = (newMap[secondHalf] ?: 0) + amount
} else {
newMap[number * 2024] = (newMap[number * 2024] ?: 0) + amount
}
}
map = newMap
}
return map.values.sum()
}
}

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

0 comments on commit 00f6e43

Please sign in to comment.