Day 19 part 1
This commit is contained in:
parent
4807b2a033
commit
97fa4abec0
2 changed files with 423 additions and 0 deletions
21
2024/src/main/kotlin/day19.kt
Normal file
21
2024/src/main/kotlin/day19.kt
Normal file
|
@ -0,0 +1,21 @@
|
|||
fun main() {
|
||||
val iterator = readInput("day19.txt").iterator()
|
||||
|
||||
val towels = Sequence { iterator }.takeWhile { it.isNotEmpty() }
|
||||
.flatMap { it.split(",\\s*".toRegex()) }
|
||||
.toList()
|
||||
|
||||
val part1 = Sequence { iterator }.count { isPossible(it, towels) }
|
||||
println("Part 1: $part1")
|
||||
}
|
||||
|
||||
private val patternMemo = mutableMapOf<String, Boolean>()
|
||||
|
||||
fun isPossible(pattern: String, towels: List<String>): Boolean {
|
||||
if (pattern.isEmpty()) return true
|
||||
return patternMemo.getOrPut(pattern) {
|
||||
towels
|
||||
.filter { pattern.startsWith(it) }
|
||||
.any { isPossible(pattern.substring(it.length), towels) }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue