The resulting polymer isn’t nearly strong enough to reinforce the submarine. You’ll need to run more steps of the pair insertion process; a total of 40 steps should do it.

In the above example, the most common element is B (occurring 2192039569602 times) and the least common element is H (occurring 3849876073 times); subtracting these produces 2188189693529.

Assignment

Apply 40 steps of pair insertion to the polymer template and find the most and least common elements in the result. What do you get if you take the quantity of the most common element and subtract the quantity of the least common element? Determine this in the following way:

Example

In this interactive session we assume the text files rules01.txt1 and rules02.txt2 to be located in the current directory.

> polymerize("rules01.txt")
2188189693529
> polymerize("rules02.txt")
8336623059567

Epilogue

Nazarii Bardiuk (@nbardiuk3) animated his solution.