Skip to content

Commit f58d055

Browse files
committed
Solution Day 19, part two (Linen Layout)
1 parent e6b14ca commit f58d055

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

src/main/kotlin/Day19.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ import de.ronny_h.extensions.PrefixTree
33
fun main() {
44
val day = "Day19"
55

6-
fun part1(input: List<String>): Int {
7-
val towels = input.first().split(", ")
8-
val designs = input.drop(2)
6+
fun List<String>.parseTowels(): List<String> = this.first().split(", ")
7+
fun List<String>.parseDesigns(): List<String> = drop(2)
98

9+
fun part1(input: List<String>): Int {
10+
val towels = input.parseTowels()
11+
val designs = input.parseDesigns()
1012
println("${towels.size} towels, ${designs.size} designs")
1113

1214
return designs.count { it.isPossibleWith(towels) }
1315
}
1416

15-
fun part2(input: List<String>): Int {
16-
return input.size
17+
fun part2(input: List<String>): Long {
18+
val towels = input.parseTowels()
19+
val designs = input.parseDesigns()
20+
println("${towels.size} towels, ${designs.size} designs")
21+
22+
return designs.sumOf { it.countPossibilitiesWith(towels) }
1723
}
1824

1925
println("$day part 1")
@@ -38,8 +44,9 @@ fun main() {
3844

3945
println("$day part 2")
4046

41-
printAndCheck(testInput, ::part2, 0)
42-
printAndCheck(input, ::part2, 0)
47+
printAndCheck(testInput, ::part2, 16)
48+
printAndCheck(input, ::part2, 616957151871345)
4349
}
4450

45-
private fun String.isPossibleWith(towels: List<String>) = PrefixTree().insert(this, towels)
51+
private fun String.isPossibleWith(towels: List<String>): Boolean = (this.countPossibilitiesWith(towels) > 0)
52+
private fun String.countPossibilitiesWith(towels: List<String>): Long = PrefixTree().insert(this, towels)

src/main/kotlin/de/ronny_h/extensions/PrefixTree.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@ package de.ronny_h.extensions
22

33
class PrefixTree {
44

5-
fun insert(word: String, tokens: List<String>): Boolean {
5+
fun insert(word: String, tokens: List<String>): Long {
66

7-
lateinit var insertRec: (String) -> Boolean
7+
lateinit var insertRec: (String) -> Long
88

99
insertRec = { word: String ->
1010
if (word.isEmpty()) {
11-
true
11+
1L
1212
} else {
13-
var success = false
13+
var successes = 0L
1414
for (token in tokens) {
1515
if (!word.startsWith(token)) {
1616
continue
1717
}
18-
if (insertRec(word.substring(token.length))) {
19-
success = true
20-
break
21-
}
18+
successes += insertRec(word.substring(token.length))
2219
}
23-
success
20+
successes
2421
}
2522
}.memoize()
2623

src/test/kotlin/de/ronny_h/extensions/PrefixTreeTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@ import io.kotlintest.specs.StringSpec
55

66
class PrefixTreeTest : StringSpec({
77

8-
"an empty word always can be inserted" {
8+
"an empty word always can be inserted once" {
99
val tree = PrefixTree()
10-
tree.insert("", listOf("a", "b", "c")) shouldBe true
10+
tree.insert("", listOf("a", "b", "c")) shouldBe 1
1111
}
1212

13-
"a word equal to a token can be inserted" {
13+
"a word equal to a token can be inserted once" {
1414
val tree = PrefixTree()
15-
tree.insert("abc", listOf("abc")) shouldBe true
15+
tree.insert("abc", listOf("abc")) shouldBe 1
1616
}
1717

18-
"a word consisting of single-character tokens can be inserted" {
18+
"a word consisting of single-character tokens can be inserted once" {
1919
val tree = PrefixTree()
20-
tree.insert("abc", listOf("a", "b", "c")) shouldBe true
20+
tree.insert("abc", listOf("a", "b", "c")) shouldBe 1
2121
}
2222

2323
"a word consisting with a missing token can not be inserted" {
2424
val tree = PrefixTree()
25-
tree.insert("abc", listOf("a", "c")) shouldBe false
25+
tree.insert("abc", listOf("a", "c")) shouldBe 0
2626
}
2727

28-
"a word constructed from token of different lengths can be inserted" {
28+
"a word constructed from token of different lengths can be inserted once" {
2929
val tree = PrefixTree()
30-
tree.insert("abc", listOf("ab", "c")) shouldBe true
30+
tree.insert("abc", listOf("ab", "c")) shouldBe 1
3131
}
3232

33-
"a word constructed from multiple combinations of tokens can be inserted" {
33+
"a word constructed from multiple combinations of tokens can be inserted twice" {
3434
val tree = PrefixTree()
35-
tree.insert("abc", listOf("a", "ab", "bc", "c")) shouldBe true
35+
tree.insert("abc", listOf("a", "ab", "bc", "c")) shouldBe 2
3636
}
3737
})

0 commit comments

Comments
 (0)