Powered by AppSignal & Oban Pro

Day 19 - Advent of Code 2015

lib/advent_of_code/2015/day-19.livemd

Day 19 - Advent of Code 2015

Mix.install([:kino, :benchee])
:ok

Links

Prompt

--- 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?

To begin, get your puzzle input.

--- 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?

Although it hasn't changed, you can still get your puzzle input.

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"Al => ThF\nAl => ThRnFAr\nB => BCa\nB => TiB\nB => TiRnFAr\nCa => CaCa\nCa => PB\nCa => PRnFAr\nCa => SiRnFYFAr\nCa => SiRnMgAr\nCa => SiTh\nF => CaF\nF => PMg\nF => SiAl\nH => CRnAlAr\nH => CRnFYFYFAr\nH => CRnFYMgAr\nH => CRnMgYFAr\nH => HCa\nH => NRnFYFAr\nH => NRnMgAr\nH => NTh\nH => OB\nH => ORnFAr\nMg => BF\nMg => TiMg\nN => CRnFAr\nN => HSi\nO => CRnFYFAr\nO => CRnMgAr\nO => HP\nO => NRnFAr\nO => OTi\nP => CaP\nP => PTi\nP => SiRnFAr\nSi => CaSi\nTh => ThCa\nTi => BP\nTi => TiTi\ne => HF\ne => NAl\ne => OMg\n\nORnPBPMgArCaCaCaSiThCaCaSiThCaCaPBSiRnFArRnFArCaCaSiThCaCaSiThCaCaCaCaCaCaSiRnFYFArSiRnMgArCaSiRnPTiTiBFYPBFArSiRnCaSiRnTiRnFArSiAlArPTiBPTiRnCaSiAlArCaPTiTiBPMgYFArPTiRnFArSiRnCaCaFArRnCaFArCaSiRnSiRnMgArFYCaSiRnMgArCaCaSiThPRnFArPBCaSiRnMgArCaCaSiThCaSiRnTiMgArFArSiThSiThCaCaSiRnMgArCaCaSiRnFArTiBPTiRnCaSiAlArCaPTiRnFArPBPBCaCaSiThCaPBSiThPRnFArSiThCaSiThCaSiThCaPTiBSiRnFYFArCaCaPRnFArPBCaCaPBSiRnTiRnFArCaPRnFArSiRnCaCaCaSiThCaRnCaFArYCaSiRnFArBCaCaCaSiThFArPBFArCaSiRnFArRnCaCaCaFArSiRnFArTiRnPMgArF"

Solution

defmodule Day19 do
  defdelegate parse(input), to: __MODULE__.Input

  def part1(input) do
    {s, replacements} = parse(input)

    new_strings(s, replacements) |> length()
  end

  def part2(input) do
    {s, replacements} = parse(input)

    make_molecule(["e"], s, replacements, 1)
  end

  defp make_molecule(strings, goal_s, replacements, steps) do
    IO.inspect(steps, label: "step #")
    IO.inspect(Enum.at(strings, 0), label: "sample string")
    IO.inspect(length(strings), label: "num strings")

    strings
    |> Enum.flat_map(&new_strings(&1, replacements))
    |> Enum.uniq()
    |> then(fn new_list ->
      if Enum.member?(new_list, goal_s) do
        steps
      else
        make_molecule(new_list, goal_s, replacements, steps + 1)
      end
    end)
  end

  defp new_strings(s, replacements) do
    replacements
    |> Enum.flat_map(fn {find, replace} ->
      Regex.scan(~r/#{find}/, s, return: :index)
      |> List.flatten()
      |> Enum.map(fn
        {0, l} -> replace <> String.slice(s, l..-1)
        {i, l} -> String.slice(s, 0..(i - 1)) <> replace <> String.slice(s, (i + l)..-1)
      end)
    end)
    |> Enum.uniq()
  end

  defmodule Input do
    def parse(input) when is_binary(input) do
      [replacements, start] = String.split(input, "\n\n")

      {start, replacements |> String.split("\n") |> Enum.map(&parse_line/1)}
    end

    def parse(input) when is_list(input) do
      Stream.map(input, &parse_line/1)
    end

    def parse_line(line) do
      String.split(line, " => ") |> List.to_tuple()
    end
  end
end
{:module, Day19, <<70, 79, 82, 49, 0, 0, 17, ...>>,
 {:module, Day19.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}

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 576.

Day19.part1(input)
576

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?

Your puzzle answer was answer two.

Day19.part2(input)
step #: 1
sample string: "e"
num strings: 1
step #: 2
sample string: "HF"
num strings: 3
step #: 3
sample string: "HCaF"
num strings: 18
step #: 4
sample string: "HCaCaF"
num strings: 105
step #: 5
sample string: "HCaCaCaF"
num strings: 607
step #: 6
sample string: "HCaCaCaCaF"
num strings: 3566
step #: 7
sample string: "HCaCaCaCaCaF"
num strings: 21287
step #: 8
sample string: "HCaCaCaCaCaCaF"
num strings: 129071

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.

Tests

ExUnit.start(auto_run: false)

defmodule Day19Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input1: "e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOH",
      input2: "e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOHOHO"
    ]
  end

  describe "part1/1" do
    test "returns expected value", inputs do
      assert Day19.part1(inputs.input1) == 4
      assert Day19.part1(inputs.input2) == 7
    end
  end

  describe "part2/1" do
    test "returns expected value", inputs do
      assert Day19.part2(inputs.input1) == 3
      assert Day19.part2(inputs.input2) == 6
    end
  end
end

ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures

Randomized with seed 597974
%{excluded: 0, failures: 0, skipped: 0, total: 2}

Benchmarking

# https://github.com/bencheeorg/benchee

Benchee.run(
  %{
    "Part 1" => fn -> Day19.part1(input) end,
    "Part 2" => fn -> Day19.part2(input) end
  },
  memory_time: 2,
  reduction_time: 2
)

nil

nil