Skip to content

Commit 786cd0e

Browse files
committed
Solution 2015-19 (Medicine for Rudolph)
For part two and my puzzle input, a solution is printed very quickly. But then it takes an unacceptable amount of time to verify that this solution is the minimal one.
1 parent cc82fe0 commit 786cd0e

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package de.ronny_h.aoc.year2015.day19
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.memoize
5+
import de.ronny_h.aoc.extensions.split
6+
7+
fun main() = MedicineForRudolph().run(518, 200)
8+
9+
class MedicineForRudolph : AdventOfCode<Int>(2015, 19) {
10+
override fun part1(input: List<String>): Int {
11+
val (replacementStrings, moleculeString) = input.split()
12+
val replacements = replacementStrings.parseReplacements()
13+
val startMolecule = moleculeString.single()
14+
15+
val producedMolecules = mutableSetOf<String>()
16+
replacements.forEach { replacement ->
17+
replacement.from
18+
.toRegex()
19+
.findAll(startMolecule)
20+
.forEach {
21+
producedMolecules.add(
22+
startMolecule.substring(
23+
0,
24+
it.range.start
25+
) + replacement.to + startMolecule.substring(it.range.endInclusive + 1)
26+
)
27+
}
28+
}
29+
30+
return producedMolecules.size
31+
}
32+
33+
override fun part2(input: List<String>): Int {
34+
val (replacementStrings, moleculeString) = input.split()
35+
return produceMolecule(replacementStrings.parseReplacements(), moleculeString.single())
36+
}
37+
38+
// For my input, a solution is printed very quickly. But then it takes an unacceptable amount of time to verify
39+
// that this solution is the minimal one.
40+
private fun produceMolecule(
41+
replacements: List<Replacement>,
42+
molecule: String,
43+
): Int {
44+
data class Arguments(val molecule: String, val steps: Int)
45+
46+
lateinit var produceMolecule: (Arguments) -> Int?
47+
48+
produceMolecule = { arg: Arguments ->
49+
if (arg.molecule == "e") {
50+
println(arg.steps)
51+
arg.steps
52+
} else {
53+
replacements.mapNotNull { replacement ->
54+
replacement.to
55+
.toRegex()
56+
.findAll(arg.molecule)
57+
.mapNotNull {
58+
produceMolecule(
59+
Arguments(
60+
arg.molecule.substring(
61+
0,
62+
it.range.start
63+
) + replacement.from + arg.molecule.substring(it.range.endInclusive + 1),
64+
arg.steps + 1
65+
)
66+
)
67+
}.minOrNull()
68+
69+
}.minOrNull()
70+
}
71+
}.memoize()
72+
73+
return produceMolecule(Arguments(molecule, 0))!!
74+
}
75+
}
76+
77+
data class Replacement(val from: String, val to: String)
78+
79+
fun List<String>.parseReplacements() = map {
80+
val (from, to) = it.split(" => ")
81+
Replacement(from, to)
82+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package de.ronny_h.aoc.year2015.day19
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class MedicineForRudolphTest : StringSpec({
7+
8+
val replacements1 = listOf(
9+
"H => HO",
10+
"H => OH",
11+
"O => HH",
12+
)
13+
14+
"replacements can be parsed" {
15+
replacements1.parseReplacements() shouldBe listOf(
16+
Replacement("H", "HO"),
17+
Replacement("H", "OH"),
18+
Replacement("O", "HH"),
19+
)
20+
}
21+
22+
"part 1: there are 4 distinct molecules produced from HOH" {
23+
val input = replacements1 + listOf("", "HOH")
24+
MedicineForRudolph().part1(input) shouldBe 4
25+
}
26+
27+
"part 1: there are 7 distinct molecules produced from HOHOHO" {
28+
val input = replacements1 + listOf("", "HOHOHO")
29+
MedicineForRudolph().part1(input) shouldBe 7
30+
}
31+
32+
val replacements2 = listOf(
33+
"e => H",
34+
"e => O",
35+
"H => HO",
36+
"H => OH",
37+
"O => HH",
38+
)
39+
40+
"part 2: there are at least 3 steps to produce HOH" {
41+
val input = replacements2 + listOf("", "HOH")
42+
MedicineForRudolph().part2(input) shouldBe 3
43+
}
44+
45+
"part 2: there are at least 3 steps to produce HOHOHO" {
46+
val input = replacements2 + listOf("", "HOHOHO")
47+
MedicineForRudolph().part2(input) shouldBe 6
48+
}
49+
})

0 commit comments

Comments
 (0)