2015 day 19.
This commit is contained in:
		
							
								
								
									
										56
									
								
								src/holt59/aoc/2015/day19.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/holt59/aoc/2015/day19.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| import sys | ||||
| from collections import defaultdict | ||||
|  | ||||
| replacements_s, molecule = sys.stdin.read().split("\n\n") | ||||
|  | ||||
| REPLACEMENTS: dict[str, list[str]] = defaultdict(list) | ||||
| for replacement_s in replacements_s.splitlines(): | ||||
|     p = replacement_s.split(" => ") | ||||
|     REPLACEMENTS[p[0]].append(p[1]) | ||||
| molecule = molecule.strip() | ||||
|  | ||||
| generated = [ | ||||
|     molecule[:i] + replacement + molecule[i + len(symbol) :] | ||||
|     for symbol, replacements in REPLACEMENTS.items() | ||||
|     for replacement in replacements | ||||
|     for i in range(len(molecule)) | ||||
|     if molecule[i:].startswith(symbol) | ||||
| ] | ||||
|  | ||||
| answer_1 = len(set(generated)) | ||||
| print(f"answer 1 is {answer_1}") | ||||
|  | ||||
| inversion: dict[str, str] = { | ||||
|     replacement: symbol | ||||
|     for symbol, replacements in REPLACEMENTS.items() | ||||
|     for replacement in replacements | ||||
| } | ||||
|  | ||||
| # there is actually only one way to create the molecule, and we can greedily replace | ||||
| # tokens with their replacements, e.g., if H => OH then we can replace OH by H directly | ||||
| # without thinking | ||||
|  | ||||
| count = 0 | ||||
| while molecule != "e": | ||||
|     i = 0 | ||||
|     m2 = "" | ||||
|     while i < len(molecule): | ||||
|         found = False | ||||
|         for replacement in inversion: | ||||
|             if molecule[i:].startswith(replacement): | ||||
|                 m2 += inversion[replacement] | ||||
|                 i += len(replacement) | ||||
|                 count += 1 | ||||
|                 found = True | ||||
|                 break | ||||
|  | ||||
|         if not found: | ||||
|             m2 += molecule[i] | ||||
|             i += 1 | ||||
|  | ||||
|     # print(m2) | ||||
|     molecule = m2 | ||||
|  | ||||
|  | ||||
| answer_2 = count | ||||
| print(f"answer 2 is {count}") | ||||
		Reference in New Issue
	
	Block a user