Advent of Code

    [About][Events][Shop][Settings][Log Out]

l x _ x l 37*
   $year=2015;

    [Calendar][AoC++][Sponsors][Leaderboard][Stats]

--- Day 19: Medicine for Rudolph ---

Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs medicine.

Red-Nosed Reindeer biology isn't similar to regular reindeer biology; Rudolph is going to need custom-made medicine. Unfortunately, Red-Nosed Reindeer chemistry isn't similar to regular reindeer chemistry, either.

The North Pole is equipped with a Red-Nosed Reindeer nuclear fusion/fission plant, capable of constructing any Red-Nosed Reindeer molecule you need. It works by starting with some input molecule and then doing a series of replacements, one per step, until it has the right molecule.

However, the machine has to be calibrated before it can be used. Calibration involves determining the number of molecules that can be generated in one step from a given starting point.

For example, imagine a simpler machine that supports only the following replacements:

H => HO
H => OH
O => HH

Given the replacements above and starting with HOH, the following molecules could be generated:

    HOOH (via H => HO on the first H).
    HOHO (via H => HO on the second H).
    OHOH (via H => OH on the first H).
    HOOH (via H => OH on the second H).
    HHHH (via O => HH).

So, in the example above, there are 4 distinct molecules (not five, because HOOH appears twice) after one replacement from HOH. Santa's favorite molecule, HOHOHO, can become 7 distinct molecules (over nine replacements: six from H, and three from O).

The machine replaces without regard for the surrounding characters. For example, given the string H2O, the transition H => OO would result in OO2O.

Your puzzle input describes all of the possible replacements and, at the bottom, the medicine molecule for which you need to calibrate the machine. How many distinct molecules can be created after all the different ways you can do one replacement on the medicine molecule?

Your puzzle answer was 535.

The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---

Now that the machine is calibrated, you're ready to begin molecule fabrication.

Molecule fabrication always begins with just a single electron, e, and applying replacements one at a time, just like the ones during calibration.

For example, suppose you have the following replacements:

e => H
e => O
H => HO
H => OH
O => HH

If you'd like to make HOH, you start with e, and then make the following replacements:

    e => O to get O
    O => HH to get HH
    H => OH (on the second H) to get HOH

So, you could make HOH after 3 steps. Santa's favorite molecule, HOHOHO, can be made in 6 steps.

How long will it take to make the medicine? Given the available replacements and the medicine molecule in your puzzle input, what is the fewest number of steps to go from e to the medicine molecule?

```rust
use std::collections::{HashMap, VecDeque};

fn parse_input(input: &str) -> (HashMap<String, Vec<String>>, String) {
    let mut lines = input.lines();
    let mut replacements = HashMap::new();
    let mut molecule = String::new();

    while let Some(line) = lines.next() {
        if line.trim().is_empty() {
            break;
        }

        let parts: Vec<&str> = line.split(" => ").collect();
        let (left, right) = (parts[0].to_string(), parts[1].to_string());
        replacements.entry(right).or_insert(Vec::new()).push(left);
    }

    if let Some(line) = lines.next() {
        molecule = line.trim().to_string();
    }

    (replacements, molecule)
}

fn minimum_transformation_steps(replacements: HashMap<String, Vec<String>>, target: &str) -> usize {
    let mut queue = VecDeque::new();
    let mut seen = HashMap::new();

    queue.push_back(target.to_string());
    seen.insert(target.to_string(), 0);

    while let Some(current) = queue.pop_front() {
        let steps = seen[&current];
        if &current == "e" {
            return steps;
        }

        for (output, inputs) in &replacements {
            for (i, _) in current.match_indices(output) {
                for input in inputs {
                    let mut new_molecule = current.clone();
                    new_molecule.replace_range(i..i + output.len(), input);
                    if !seen.contains_key(&new_molecule) {
                        seen.insert(new_molecule.clone(), steps + 1);
                        queue.push_back(new_molecule);
                    }
                }
            }
        }
    }

    usize::MAX // If not found, return max indicating impossibility
}

fn main() {
    let input = r#"
e => H
e => O
H => HO
H => OH
O => HH
XY => ABD
BD => BC
Z => DEF
HOHO => XY
HO => Z

ABCDEFABCDEFHO
"#
    .trim();
    let (replacements, molecule) = parse_input(input);
    let steps = minimum_transformation_steps(replacements, &molecule);
    println!("Minimum number of steps: {}", steps);
}
```

Do all the additional optimizations for substantially larger input cases