advent_of_code/14/14.py
kleph d2af5b8bad
Some checks failed
continuous-integration/drone/push Build is failing
lint
2022-04-05 14:14:12 +02:00

46 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python
# 2021 - Advent Of Code - 14
from collections import Counter
def parse_file(file):
with open(file, encoding="utf-8") 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 = []
p = None
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(13):
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)