37 lines
726 B
Python
37 lines
726 B
Python
rules = {}
|
|
polymer = []
|
|
with open("sample.txt") as f:
|
|
template, instructions = f.read().split('\n\n')
|
|
for element in template:
|
|
polymer.append(element)
|
|
for rule in instructions.split('\n'):
|
|
a, b = rule.split(' -> ')
|
|
rules[a] = b
|
|
|
|
def step(polymer):
|
|
polymer2 = []
|
|
for i, element in enumerate(polymer):
|
|
polymer2.append(element)
|
|
if i < len(polymer) - 1:
|
|
|
|
pair = element + polymer[i+1]
|
|
if pair in rules:
|
|
insertion = rules[pair]
|
|
polymer2.append(insertion)
|
|
return polymer2
|
|
|
|
|
|
part1 = polymer.copy()
|
|
for i in range(10):
|
|
part1 = step(part1)
|
|
|
|
from collections import Counter
|
|
c = Counter(part1)
|
|
f = c.most_common()
|
|
most = f[0][1]
|
|
least = f[-1][1]
|
|
print(most - least)
|
|
|
|
c2 = Counter()
|
|
def step2(polymer):
|
|
|