Skip to content

Commit e6b14ca

Browse files
committed
Solution Day 19, part one (Linen Layout)
* Just check if the word can be constructed with the given tokens and don't construct a prefix tree in memory. * Use memoize() to exploit information of already examined sub-trees.
1 parent f6081a9 commit e6b14ca

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/main/kotlin/Day19.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import de.ronny_h.extensions.PrefixTree
2+
3+
fun main() {
4+
val day = "Day19"
5+
6+
fun part1(input: List<String>): Int {
7+
val towels = input.first().split(", ")
8+
val designs = input.drop(2)
9+
10+
println("${towels.size} towels, ${designs.size} designs")
11+
12+
return designs.count { it.isPossibleWith(towels) }
13+
}
14+
15+
fun part2(input: List<String>): Int {
16+
return input.size
17+
}
18+
19+
println("$day part 1")
20+
21+
val testInput = """
22+
r, wr, b, g, bwu, rb, gb, br
23+
24+
brwrr
25+
bggr
26+
gbbr
27+
rrbgbr
28+
ubwu
29+
bwurrg
30+
brgr
31+
bbrgwb
32+
""".trimIndent().split('\n')
33+
printAndCheck(testInput, ::part1, 6)
34+
35+
val input = readInput(day)
36+
printAndCheck(input, ::part1, 251)
37+
38+
39+
println("$day part 2")
40+
41+
printAndCheck(testInput, ::part2, 0)
42+
printAndCheck(input, ::part2, 0)
43+
}
44+
45+
private fun String.isPossibleWith(towels: List<String>) = PrefixTree().insert(this, towels)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package de.ronny_h.extensions
2+
3+
class PrefixTree {
4+
5+
fun insert(word: String, tokens: List<String>): Boolean {
6+
7+
lateinit var insertRec: (String) -> Boolean
8+
9+
insertRec = { word: String ->
10+
if (word.isEmpty()) {
11+
true
12+
} else {
13+
var success = false
14+
for (token in tokens) {
15+
if (!word.startsWith(token)) {
16+
continue
17+
}
18+
if (insertRec(word.substring(token.length))) {
19+
success = true
20+
break
21+
}
22+
}
23+
success
24+
}
25+
}.memoize()
26+
27+
return insertRec(word)
28+
}
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package de.ronny_h.extensions
2+
3+
import io.kotlintest.shouldBe
4+
import io.kotlintest.specs.StringSpec
5+
6+
class PrefixTreeTest : StringSpec({
7+
8+
"an empty word always can be inserted" {
9+
val tree = PrefixTree()
10+
tree.insert("", listOf("a", "b", "c")) shouldBe true
11+
}
12+
13+
"a word equal to a token can be inserted" {
14+
val tree = PrefixTree()
15+
tree.insert("abc", listOf("abc")) shouldBe true
16+
}
17+
18+
"a word consisting of single-character tokens can be inserted" {
19+
val tree = PrefixTree()
20+
tree.insert("abc", listOf("a", "b", "c")) shouldBe true
21+
}
22+
23+
"a word consisting with a missing token can not be inserted" {
24+
val tree = PrefixTree()
25+
tree.insert("abc", listOf("a", "c")) shouldBe false
26+
}
27+
28+
"a word constructed from token of different lengths can be inserted" {
29+
val tree = PrefixTree()
30+
tree.insert("abc", listOf("ab", "c")) shouldBe true
31+
}
32+
33+
"a word constructed from multiple combinations of tokens can be inserted" {
34+
val tree = PrefixTree()
35+
tree.insert("abc", listOf("a", "ab", "bc", "c")) shouldBe true
36+
}
37+
})

0 commit comments

Comments
 (0)