Skip to content

Commit

Permalink
Day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
hibob224 committed Dec 19, 2024
1 parent ef3c0e7 commit f19446f
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/main/kotlin/y2024/day19/Day19.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,20 @@ object Day19 {

fun solvePartOne(): Int {
return desiredTowels.count {
isTowelPossible("", it)
isTowelPossible(it)
}
}

private fun isTowelPossible(currentTowel: String, desired: String): Boolean {
return towelSupply.any {
val stitched = currentTowel + it
if (stitched == desired) {
true
} else if (!desired.startsWith(stitched)) {
false
} else {
isTowelPossible(currentTowel + it, desired)
private fun isTowelPossible(desired: String): Boolean =
towelSupply.any {
when {
it == desired -> true
desired.startsWith(it) -> isTowelPossible(desired.removePrefix(it))
else -> false
}
}
}

fun solvePartTwo(): Long = desiredTowels.sumOf {
optionCount(it)
}
fun solvePartTwo(): Long = desiredTowels.sumOf(::optionCount)

private fun optionCount(
desired: String,
Expand Down

0 comments on commit f19446f

Please sign in to comment.