Day 12 :-/
This commit is contained in:
parent
ee3629989b
commit
9ececeeb43
@ -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
|
fun fits(input: String, regs: IntArray, inputPos: Int, regPos: Int): Boolean {
|
||||||
var currRegPos = regPos
|
if (regPos > regs.lastIndex || inputPos + regs[regPos] > input.length) return false
|
||||||
for ((i, p) in input.withIndex()) {
|
var ip = inputPos
|
||||||
when (p) {
|
var e = regs[regPos]
|
||||||
'.' ->
|
if (input[ip] == '.') ip++
|
||||||
if (currRegPos >= 0 && match != regs[currRegPos]) {
|
while ((e > 0) && ip <= input.lastIndex && input[ip++] != '.') {
|
||||||
return 0
|
e--
|
||||||
} else {
|
|
||||||
if (match > 0) {
|
|
||||||
currRegPos--
|
|
||||||
}
|
|
||||||
if (i == input.lastIndex) {
|
|
||||||
return if (currRegPos < 0) comb else 0
|
|
||||||
}
|
}
|
||||||
|
if (e > 0) return false
|
||||||
|
return ip > input.lastIndex || input[ip] != '#'
|
||||||
}
|
}
|
||||||
|
|
||||||
'#' -> if (currRegPos < 0) {
|
fun count(input: String, regs: IntArray, inputPos: Int, regPos: Int): Long {
|
||||||
return 0
|
val memVal = memo[inputPos * 1000 + regPos]
|
||||||
} else {
|
if (memVal != null) return memVal
|
||||||
match++
|
|
||||||
if (match > regs[currRegPos]) {
|
//println("$inputPos, $regPos, ${input.substring(inputPos)}")
|
||||||
return 0
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
'?' -> if (currRegPos < 0) {
|
fun part1(input: List<String>): Long {
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
fun part1(input: List<String>): Int {
|
|
||||||
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user