1
0
Fork 0

day(24): add solution

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-24 21:48:29 +01:00
parent 13b2f0fb92
commit a8b68b19b9
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
4 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1,46 @@
package year2021.day24
import readInput
data class Parameter(val remainder: Long, val divisor: Long, val addition: Long)
fun solver(range: LongProgression, parameters: List<Parameter>): Long {
val cache: MutableMap<Triple<Int, Long, Long>, Long?> = mutableMapOf()
fun solveAlternative(i: Int, w: Long, z: Long, overall: Long = 0): Long? {
val cacheKey = Triple(i, w, z)
if (cacheKey in cache) {
return cache[cacheKey];
}
val (remainder, divisor, addition) = parameters[i]
val x = if (((z % 26) + remainder) != w) 1 else 0
val newZ = z / divisor * (25 * x + 1) + (w + addition) * x
val result = when (i) {
13 -> if (newZ == 0L) overall else null
else -> range.firstNotNullOfOrNull {
solveAlternative(i + 1, it, newZ, overall * 10 + it)
}
}
cache[cacheKey] = result
return result
}
return range.firstNotNullOf { solveAlternative(0, it, 0, it) }
}
fun part1(parameters: List<Parameter>): Long = solver(9L downTo 1L, parameters)
fun part2(parameters: List<Parameter>): Long = solver(1L..9L, parameters)
fun List<String>.toParameters(): List<Parameter> = this.chunked(18).map {
val (rem, div, add) = listOf(it[5], it[4], it[15]).map { it.split(" ")[2].toLong() }
Parameter(rem, div, add)
}
fun main() {
val parameters = readInput(24, "input").toParameters()
println(part1(parameters))
println(part2(parameters))
}

View file

@ -0,0 +1,11 @@
inp w
add z w
mod z 2
div w 2
add y w
mod y 2
div w 2
add x w
mod x 2
div w 2
mod w 2

View file

@ -0,0 +1,2 @@
inp x
mul x -1

View file

@ -0,0 +1,4 @@
inp z
inp x
mul z 3
eql z x