summary refs log blame commit diff stats
path: root/day19.py
blob: d7b92ee6dbcea8dcde1fbae659c14a1759a2832d (plain) (tree)



























                                                                                                
#!/usr/bin/env python

import re
from collections import defaultdict

replacements = []
starter = None
with open('day19.txt') as data:
    for line in data:
        groups = re.search(r'(\w+) => (\w+)', line)
        if groups:
            replacements.append(groups.groups((1, 2)))
        else:
            if line != '\n':
                starter = line.strip()

# part 1
compounds = set()
for elem, repl in replacements:
    for i in range(len(starter)):
        if starter[i:i+len(elem)] == elem:
            compound = starter[:i] + repl + starter[i+len(elem):]
            compounds.add(compound)
print(len(compounds))

# part 2
elements = [s for s in re.split(r'([A-Z][^A-Z]*)', starter) if s]
print(len(elements) - elements.count('Ar') - elements.count('Rn') - 2 * elements.count('Y') - 1)