Skip to content

Commit

Permalink
2024 Day 11, WIP...
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongXiLu committed Dec 11, 2024
1 parent b89ef6f commit 72ee5be
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/kotlin/aoc/years/year2024/Day11.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package aoc.years.year2024

import aoc.Day
import java.math.BigInteger

@Year2024
class Day11 : Day() {

override fun solvePart1(input: List<String>): Any {
val memoization = HashMap<BigInteger, BigInteger>()
return getStones(input).sumOf { it.getNrOfStonesAfter(25, memoization) }
}

override fun solvePart2(input: List<String>): Any {
// try recursive with memoization?
val memoization = HashMap<BigInteger, BigInteger>()
return getStones(input).sumOf { it.getNrOfStonesAfter(25, memoization) }
}

private fun getStones(input: List<String>) = input.first().split(" ").map { it.toBigInteger() }
}

private fun BigInteger.getNrOfStonesAfter(blinks: Int, memoization: MutableMap<BigInteger, BigInteger>): Int {
if (blinks == 0) {
return 1
}

if (this == BigInteger.ZERO) {
val nrOfStones = BigInteger.ONE.getNrOfStonesAfter(blinks - 1, memoization)
//
return nrOfStones
} else {
val digit = this.toString()
if (digit.length % 2 == 0) {
val firstStone = digit.substring(0, digit.length / 2).toBigInteger()
val secondStone = digit.substring(digit.length / 2).toBigInteger()
return firstStone.getNrOfStonesAfter(blinks - 1, memoization) + secondStone.getNrOfStonesAfter(blinks - 1, memoization)
} else {
return (this * BigInteger.valueOf(2024)).getNrOfStonesAfter(blinks - 1, memoization)
}
}
}

private fun List<BigInteger>.blink(): List<BigInteger> {
return this.map {
val digit = it.toString()
if (it == BigInteger.ZERO) {
return@map listOf(BigInteger.ONE)
} else {
if (digit.length % 2 == 0) {
val firstStone = digit.substring(0, digit.length / 2).toBigInteger()
val secondStone = digit.substring(digit.length / 2).toBigInteger()
return@map listOf(firstStone, secondStone)
} else {
return@map listOf(it * BigInteger.valueOf(2024))
}
}
}.flatten()
}
1 change: 1 addition & 0 deletions src/main/resources/aoc/year2024/Day11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
572556 22 0 528 4679021 1 10725 2790

0 comments on commit 72ee5be

Please sign in to comment.