day05: Day 05: Alchemical Reduction

Description Usage Arguments Details Value Examples

Description

Alchemical Reduction

Usage

1
2
3
4
5

Arguments

x

a string describing a polymer

verbose

whether to print out the each step of the polymer simulation

Details

Part One

You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.

While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is [available]title="I've always wanted a polymer!" as your puzzle input).

The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.

For example:

Now, consider a larger example, dabAcCaCBAcCcaDA:

1
2
3
4
dabAcCaCBAcCcaDA  The first 'cC' is removed.
dabAaCBAcCcaDA    This creates 'Aa', which is removed.
dabCBAcCcaDA      Either 'cC' or 'Cc' are removed (the result is the same).
dabCBAcaDA        No further actions can be taken.

After all possible reactions, the resulting polymer contains 10 units.

How many units remain after fully reacting the polymer you scanned? [(Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)].quiet

Part Two

Time to improve the polymer.

One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.

For example, again using the polymer dabAcCaCBAcCcaDA from above:

In this example, removing all C/c units was best, producing the answer 4.

What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?

Value

For Part One, create_polymer(x) returns a polymer object that can run a reaction. run_polymer_reaction(x) returns the polymer after all reactions are finished. For Part Two, simulate_polymer_reactions(x) returns a list with the length of the polymer after removing each unit and letting the resulting polymer react.

Examples

1
2
3
4
5
6
7
p <- create_polymer("dabAcCaCBAcCcaDA")
p$run_reaction()
# or
run_polymer_reaction("dabAcCaCBAcCcaDA")
run_polymer_reaction("dabAcCaCBAcCcaDA", verbose = TRUE)

simulate_polymer_reactions(x)

tjmahr/adventofcode18 documentation built on May 24, 2019, 4:10 p.m.