part1 (2801B)
1--- Day 14: Extended Polymerization --- 2 3The incredible pressures at this depth are starting to put a strain on your submarine. The submarine 4has polymerization equipment that would produce suitable materials to reinforce the submarine, and 5the nearby volcanically-active caves should even have the necessary input elements in sufficient 6quantities. 7 8The submarine manual contains instructions for finding the optimal polymer formula; specifically, it 9offers a [1m[97mpolymer template[0m and a list of [1m[97mpair insertion[0m rules (your puzzle input). You just need to 10work out what polymer would result after repeating the pair insertion process a few times. 11 12For example: 13 14NNCB 15 16CH -> B 17HH -> N 18CB -> H 19NH -> C 20HB -> C 21HC -> B 22HN -> C 23NN -> C 24BH -> H 25NC -> B 26NB -> B 27BN -> B 28BB -> N 29BC -> B 30CC -> N 31CN -> C 32 33The first line is the [1m[97mpolymer template[0m - this is the starting point of the process. 34 35The following section defines the [1m[97mpair insertion[0m rules. A rule like AB -> C means that when elements 36A and B are immediately adjacent, element C should be inserted between them. These insertions all 37happen simultaneously. 38 39So, starting with the polymer template NNCB, the first step simultaneously considers all three 40pairs: 41 42 43 - The first pair (NN) matches the rule NN -> C, so element [1m[97mC[0m is inserted between the first N and 44the second N. 45 46 - The second pair (NC) matches the rule NC -> B, so element [1m[97mB[0m is inserted between the N and the C. 47 48 - The third pair (CB) matches the rule CB -> H, so element [1m[97mH[0m is inserted between the C and the B. 49 50 51Note that these pairs overlap: the second element of one pair is the first element of the next pair. 52Also, because all pairs are considered simultaneously, inserted elements are not considered to be 53part of a pair until the next step. 54 55After the first step of this process, the polymer becomes 56N[1m[97mC[0mN[1m[97mB[0mC[1m[97mH[0mB. 57 58Here are the results of a few steps using the above rules: 59 60Template: NNCB 61After step 1: NCNBCHB 62After step 2: NBCCNBBBCBHCB 63After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB 64After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB 65 66This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After 67step 10, B occurs 1749 times, C occurs 298 times, H occurs 161 times, and N occurs 865 times; taking 68the quantity of the most common element (B, 1749) and subtracting the quantity of the least common 69element (H, 161) produces 1749 - 161 = [1m[97m1588[0m. 70 71Apply 10 steps of pair insertion to the polymer template and find the most and least common elements 72in the result. [1m[97mWhat do you get if you take the quantity of the most common element and subtract the 73quantity of the least common element?[0m 74 75