from collections import defaultdict with open("input") as f: start_number = f.readline().strip() print("Start:", start_number, "length:", len(start_number)) ATOM_TO_NUMBER = dict() NUMBER_TO_ATOM = dict() DECAY = dict() # From https://web.archive.org/web/20061224154744/http://www.uam.es/personal_pdi/ciencias/omartin/Biochem.PDF with open("atoms") as f: for line in f.readlines(): columns = line.strip().split(" ") [_, atom, number] = columns[:3] NUMBER_TO_ATOM[number] = atom ATOM_TO_NUMBER[atom] = number DECAY[atom] = columns[3:] start_atom = NUMBER_TO_ATOM[start_number] print("Starting with atom:", start_atom) ATOM_COUNT = defaultdict(int) ATOM_COUNT[start_atom] = 1 for i in range(50): NEXT_ATOM_COUNT = defaultdict(int) for atom, count in ATOM_COUNT.items(): for new_atom in DECAY[atom]: NEXT_ATOM_COUNT[new_atom] += count ATOM_COUNT = NEXT_ATOM_COUNT length = 0 for atom, count in ATOM_COUNT.items(): length += len(ATOM_TO_NUMBER[atom]) * count print("Round", i + 1, "len:", length) print(length)