# \--- AOC Day 14: Extended Polymerization ---

**URL:** <https://forum.kx.com/t/aoc-day-14-extended-polymerization/12321>\
**Category:** Community Support\
**Tags:** advent-of-code\
**Created:** [December 14, 2021, 9:32am UTC](https://forum.kx.com/t/aoc-day-14-extended-polymerization/12321 "2021-12-14T09:32:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laura2](https://avatars.discourse-cdn.com/v4/letter/l/76d3ee/32.png) [@Laura2](https://forum.kx.com/u/Laura2)\
**Post date:** [December 14, 2021, 9:32am UTC](https://forum.kx.com/t/aoc-day-14-extended-polymerization/12321/1 "2021-12-14T09:32:00Z")

</div>

The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has&nbsp;polymerization&nbsp;equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities.

The submarine manual contains&nbsp;instructions&nbsp;for finding the optimal polymer formula; specifically, it offers a&nbsp;_polymer template_&nbsp;and a list of&nbsp;_pair insertion_&nbsp;rules (your puzzle input). You just need to work out what polymer would result after repeating the pair insertion process a few times.

For example:

```
NNCB CH -> B HH -> N CB -> H NH -> C HB -> C HC -> B HN -> C NN -> C BH -> H NC -> B NB -> B BN -> B BB -> N BC -> B CC -> N CN -> C

```

The first line is the&nbsp;_polymer template_&nbsp;- this is the starting point of the process.

The following section defines the&nbsp;_pair insertion_&nbsp;rules. A rule like&nbsp;`AB -> C`&nbsp;means that when elements&nbsp;`A`&nbsp;and&nbsp;`B`&nbsp;are immediately adjacent, element&nbsp;`C`&nbsp;should be inserted between them. These insertions all happen simultaneously.

So, starting with the polymer template&nbsp;`NNCB`, the first step simultaneously considers all three pairs:

- The first pair (`NN`) matches the rule&nbsp;`NN -> C`, so element&nbsp;`C`&nbsp;is inserted between the first&nbsp;`N`&nbsp;and the second&nbsp;`N`.
- The second pair (`NC`) matches the rule&nbsp;`NC -> B`, so element&nbsp;`B`&nbsp;is inserted between the&nbsp;`N`&nbsp;and the&nbsp;`C`.
- The third pair (`CB`) matches the rule&nbsp;`CB -> H`, so element&nbsp;`H`&nbsp;is inserted between the&nbsp;`C`&nbsp;and the&nbsp;`B`.

Note that these pairs overlap: the second element of one pair is the first element of the next pair. Also, because all pairs are considered simultaneously, inserted elements are not considered to be part of a pair until the next step.

After the first step of this process, the polymer becomes&nbsp;`NCNBCHB`.

Here are the results of a few steps using the above rules:

```
Template: NNCB After step 1: NCNBCHB After step 2: NBCCNBBBCBHCB After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB

```

This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After step 10,&nbsp;`B`&nbsp;occurs 1749 times,&nbsp;`C`&nbsp;occurs 298 times,&nbsp;`H`&nbsp;occurs 161 times, and&nbsp;`N`&nbsp;occurs 865 times; taking the quantity of the most common element (`B`, 1749) and subtracting the quantity of the least common element (`H`, 161) produces&nbsp;`1749 - 161 = 1588`.

Apply 10 steps of pair insertion to the polymer template and find the most and least common elements in the result.&nbsp;_What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?_

&nbsp;

**All credit for the above puzzle goes to Eric Wastl and Advent of Code.**

**Don’t forget to submit your solutions at&nbsp;adventofcode.com\*\*\*\*&nbsp;and join the KX leaderboard using code&nbsp;1526329-bb0915a5.**
