advent_of_code/14/14.py

46 lines
1.1 KiB
Python
Raw Normal View History

2021-12-14 14:39:41 +01:00
#!/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}')
2022-04-05 12:35:16 +02:00
for s in range(13):
2021-12-14 14:39:41 +01:00
print(f'step: {s}')
poly = step_poly(poly, rules)
2022-04-05 12:35:16 +02:00
#print(poly)
2021-12-14 14:39:41 +01:00
print(Counter(poly))
c = Counter(poly).most_common()[0][1] - Counter(poly).most_common()[-1][1]
print(c)