Day 12 :-/

This commit is contained in:
Chris Hodges 2023-12-12 20:34:54 +01:00
parent ee3629989b
commit 9ececeeb43

View File

@ -72,70 +72,62 @@ fun main() {
?###???????? 3,2,1 ?###???????? 3,2,1
""" """
fun solve(input: String, preMatch: Int, regs: List<Int>, regPos: Int): Int { val memo = HashMap<Int, Long>()
var match = preMatch
var comb = 1
var currRegPos = regPos
for ((i, p) in input.withIndex()) {
when (p) {
'.' ->
if (currRegPos >= 0 && match != regs[currRegPos]) {
return 0
} else {
if (match > 0) {
currRegPos--
}
if (i == input.lastIndex) {
return if (currRegPos < 0) comb else 0
}
}
'#' -> if (currRegPos < 0) { fun fits(input: String, regs: IntArray, inputPos: Int, regPos: Int): Boolean {
return 0 if (regPos > regs.lastIndex || inputPos + regs[regPos] > input.length) return false
} else { var ip = inputPos
match++ var e = regs[regPos]
if (match > regs[currRegPos]) { if (input[ip] == '.') ip++
return 0 while ((e > 0) && ip <= input.lastIndex && input[ip++] != '.') {
} e--
}
'?' -> if (currRegPos < 0) {
return if (input.subSequence(i, input.length).indexOf("#") < 0) comb else 0
} else {
// if it was a hash
val s1 = if (match + 1 <= regs[currRegPos]) {
solve(input.substring(i + 1), match + 1, regs, currRegPos)
} else {
0
}
val s2 = if (match != regs[currRegPos]) {
0
} else {
if (i == input.lastIndex) {
if (currRegPos == 0) comb else 0
} else {
solve(input.substring(i + 1), 0, regs, currRegPos - 1)
}
}
comb *= (s1 + s2)
}
}
} }
return comb if (e > 0) return false
return ip > input.lastIndex || input[ip] != '#'
} }
fun part1(input: List<String>): Int { fun count(input: String, regs: IntArray, inputPos: Int, regPos: Int): Long {
val memVal = memo[inputPos * 1000 + regPos]
if (memVal != null) return memVal
//println("$inputPos, $regPos, ${input.substring(inputPos)}")
if (inputPos > input.lastIndex && regPos > regs.lastIndex) return 1
if (inputPos > input.lastIndex) return 0
var result = 0L
if (input[inputPos] != '#') {
result += count(input, regs, inputPos + 1, regPos).also { memo[(inputPos + 1) * 1000 + regPos] = it }
}
if (input[inputPos] != '.') {
if (fits(input, regs, inputPos, regPos)) {
val p = inputPos + regs[regPos] + 1
result += count(input, regs, p, regPos + 1).also { memo[p * 1000 + regPos + 1] = it }
}
}
return result
}
fun part1(input: List<String>): Long {
val results = input.map { val results = input.map {
val (springline, parms) = it.split(" ") val (springline, parms) = it.split(" ")
val reduced = springline.replace("..", ".") + "." val reduced = springline.replace("\\.+".toRegex(), ".").trim { it == '.' }
val regs = parms.split(",").map { it.toInt() }.reversed().toList() val regs = parms.split(",").map { it.toInt() }.toIntArray()
solve(reduced, 0, regs, regs.lastIndex) memo.clear()
count(reduced, regs, 0, 0)
} }
return results.sum() return results.sum()
} }
fun part2(input: List<String>): Int { fun part2(input: List<String>): Long {
return 0 val results = input.map {
val (springline, parms) = it.split(" ")
val reduced = springline.replace("\\.+".toRegex(), ".")
val regs = parms.split(",").map { it.toInt() }.toList()
val regs5 = List(5) { regs }.flatten().toIntArray()
val red5 = List(5) { reduced }.joinToString("?").trim { it == '.' }
memo.clear()
count(red5, regs5, 0, 0)
}
return results.sum()
} }
// test if implementation meets criteria from the description, like: // test if implementation meets criteria from the description, like:
@ -145,8 +137,8 @@ fun main() {
println("Part 1 Test: $testInputPart1Result") println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput) val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result") println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 21) check(testInputPart1Result == 21L)
check(testInputPart2Result == 0) check(testInputPart2Result == 525152L)
val input = readInput("aoc2023/Day12") val input = readInput("aoc2023/Day12")
part1(input).println() part1(input).println()