generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2024D19.kt
36 lines (28 loc) · 1.22 KB
/
Y2024D19.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
31
32
33
34
35
36
package aockt.y2024
import aockt.util.parse
import io.github.jadarma.aockt.core.Solution
object Y2024D19 : Solution {
/** Counts the number of ways the [model] towel can be made by arranging [patterns] together. */
private fun combos(patterns: Set<String>, model: String): Long {
val cache: MutableMap<String, Long> = mutableMapOf("" to 1L)
fun recurse(towel: String): Long = cache.getOrPut(towel) {
patterns
.filter { towel.startsWith(it) }
.sumOf { recurse(towel.removePrefix(it)) }
}
return recurse(model)
}
/** Parse the [input] and return the set of available String patterns and the list of wanted designs. */
private fun parseInput(input: String): Pair<Set<String>, List<String>> = parse {
val (patterns, models) = input.split("\n\n", limit = 2)
patterns.split(", ").toSet() to models.lines()
}
override fun partOne(input: String): Int {
val (patterns, models) = parseInput(input)
return models.count { combos(patterns, it) > 0 }
}
override fun partTwo(input: String): Long {
val (patterns, models) = parseInput(input)
return models.sumOf { combos(patterns, it) }
}
}