blob: b694c8b0557f9353391dc4fb82b7d82c251ef076 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#!/usr/bin/env 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()
|