|
| 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 | +} |
0 commit comments