-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay19LinenLayout.kt
30 lines (24 loc) · 1.03 KB
/
Day19LinenLayout.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package adventofcode.year2024
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day19LinenLayout(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val patterns by lazy { input.lines().first().split(", ") }
private val designs by lazy { input.lines().drop(2) }
override fun partOne() = designs.count { design -> design.patternCombinations(patterns) > 0 }
override fun partTwo() = designs.sumOf { design -> design.patternCombinations(patterns) }
companion object {
private fun String.patternCombinations(
patterns: List<String>,
cache: MutableMap<String, Long> = mutableMapOf(),
): Long =
when {
isEmpty() -> 1
else ->
cache.getOrPut(this) {
patterns.filter { pattern -> startsWith(pattern) }.sumOf { pattern ->
removePrefix(pattern).patternCombinations(patterns, cache)
}
}
}
}
}