-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
60 additions
and
0 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
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() | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
572556 22 0 528 4679021 1 10725 2790 |