46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
|
#!/usr/bin/env python
|
||
|
# 2021 - Advent Of Code - 14
|
||
|
|
||
|
from collections import Counter
|
||
|
|
||
|
|
||
|
def parse_file(file):
|
||
|
with open(file) as f:
|
||
|
poly_string = list(f.readline().strip())
|
||
|
f.readline()
|
||
|
insertion_rules = dict([line.strip().split(' -> ') for line in f.readlines()])
|
||
|
|
||
|
return poly_string, insertion_rules
|
||
|
|
||
|
|
||
|
def step_poly(poly_str, ruleset):
|
||
|
new_p = []
|
||
|
for i, p in enumerate(poly_str):
|
||
|
if i + 1 == len(poly_str):
|
||
|
break
|
||
|
new_p.append(p)
|
||
|
binome = '' + p + poly_str[i+1]
|
||
|
# print(f'binome {binome}')
|
||
|
found = ruleset.get(binome)
|
||
|
if found:
|
||
|
# print(f'found: {found}')
|
||
|
new_p.append(found)
|
||
|
|
||
|
new_p.append(p)
|
||
|
return ''.join(new_p)
|
||
|
|
||
|
|
||
|
# poly, rules = parse_file('input_example.txt')
|
||
|
poly, rules = parse_file('input.txt')
|
||
|
print(f'poly: {poly}')
|
||
|
print(f'rules: {rules}')
|
||
|
|
||
|
for s in range(10):
|
||
|
print(f'step: {s}')
|
||
|
poly = step_poly(poly, rules)
|
||
|
print(poly)
|
||
|
|
||
|
print(Counter(poly))
|
||
|
c = Counter(poly).most_common()[0][1] - Counter(poly).most_common()[-1][1]
|
||
|
print(c)
|