blob: 9b6760a6566d0772debf7588cd3e704ca48576bf (
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/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()
|