aboutsummaryrefslogblamecommitdiff
path: root/2015/19/puzzle-1.py
blob: 9b6760a6566d0772debf7588cd3e704ca48576bf (plain) (tree)
1
2
3
4
5
6
7
8
9
                  

         
 


                                                       





                                                                     

                                          





                                                                                                   
                                    
 
                          
#!/usr/bin/python3

import re


def main() -> None:
	replacements: dict[str, str] = {}
	with open("input", "r", encoding="utf-8") as f:
		for line in f.readlines():
			match line.strip().split(" => "):
				case [before, after]:
					while before in replacements:
						before += "_"
					replacements[before] = after
				case [mol]:
					molecule = mol

	unique_molecules: set[str] = set()
	for pattern in replacements:
		trim_pattern = pattern.rstrip("_")
		unique_molecules.update(
			molecule[: match.start()]
			+ molecule[match.start() :].replace(trim_pattern, replacements[pattern], 1)
			for match in re.finditer(trim_pattern, molecule)
		)

	print(len(unique_molecules))


if __name__ == "__main__":
	main()