aoc2021/14/a.py

42 lines
772 B
Python

from collections import defaultdict
rules = {}
# with open("sample.txt") as f:
with open("input.txt") as f:
start_poly = f.readline().strip()
f.readline()
for line in f.readlines():
l, r = line.strip().split(" -> ")
rules[l] = r
def mutate(poly):
new_poly = []
for i in range(0, len(poly)):
a = poly[i]
if i == len(poly) - 1:
new_poly.append(a)
break
b = poly[i + 1]
pair = a + b
new_poly.append(a)
new_poly.append(rules[pair])
return ''.join(new_poly)
for i in range(0, 10):
start_poly = mutate(start_poly)
counts = defaultdict(lambda: 0)
for c in start_poly:
counts[c] += 1
counts = sorted(counts.values())
print(counts[-1] - counts[0])