--- /dev/null
+""" Pair insertion """
+import collections
+
+insertion_rules = {}
+
+with open("14_input.txt") as f:
+ polymer_template = f.readline().strip()
+ f.readline()
+
+ for line in f:
+ pair, insert = line.strip().split(" -> ")
+
+ insertion_rules[pair] = insert
+
+extended_template = []
+
+expected_length = len(polymer_template)
+
+for gen in range(10):
+ for i in range(len(polymer_template) - 1):
+
+ pair = polymer_template[i:i+2]
+ extended_template += [
+ polymer_template[i],
+ insertion_rules[pair],
+ ]
+
+ polymer_template = "".join(
+ extended_template + [polymer_template[i + 1]]
+ )
+ extended_template = []
+ expected_length += expected_length - 1
+
+ assert len(polymer_template) == expected_length, "Length unexpected"
+
+letter_count = collections.defaultdict(int)
+for letter in polymer_template:
+ letter_count[letter] += 1
+
+print(max(letter_count.values()) - min(letter_count.values()))