30 lines
915 B
Python
30 lines
915 B
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
from collections import Counter
|
|
|
|
|
|
def solve(input, part, num_cycles):
|
|
result = 0
|
|
template = input[0].strip()
|
|
rules = {k: v
|
|
for line in input[2:] for k, v in [line.strip().split(' -> ')]}
|
|
counts = Counter([template[i: i + 2] for i in range(len(template) - 1)])
|
|
char_counts = Counter(template)
|
|
for _ in range(num_cycles):
|
|
for k, c in counts.copy().items():
|
|
if k in rules:
|
|
counts[k] -= c
|
|
counts[k[0] + rules[k]] += c
|
|
counts[rules[k] + k[1]] += c
|
|
char_counts[rules[k]] += c
|
|
result = char_counts.most_common(1)[0][1] - char_counts.most_common()[-1][1]
|
|
print("Part", part, "result:", result)
|
|
|
|
|
|
input = list()
|
|
p = Path(__file__).with_name('input.txt')
|
|
with open(p) as f:
|
|
input = f.readlines()
|
|
solve(input, 1, 10)
|
|
solve(input, 2, 40)
|