From 9a4ea19ae955c86312c62098830a0f0e2994196d Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 1 Dec 2021 19:15:48 +0100 Subject: Add day 19 solutions --- 2015/19/puzzle-1.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 2015/19/puzzle-1.py (limited to '2015/19/puzzle-1.py') diff --git a/2015/19/puzzle-1.py b/2015/19/puzzle-1.py new file mode 100755 index 0000000..8527ca0 --- /dev/null +++ b/2015/19/puzzle-1.py @@ -0,0 +1,30 @@ +#!/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(): + line = line.strip() + if line != "": + parts = line.split(" => ") + if len(parts) == 2: + while parts[0] in replacements: + parts[0] += "_" + replacements[parts[0]] = parts[1] + else: + molecule = line + + unique_molecules: set[str] = set() + for pattern in replacements: + trim_pattern = pattern.replace("_", "") + for match in re.finditer(trim_pattern, molecule): + before = molecule[:match.start()] + after = molecule[match.start():].replace(trim_pattern, replacements[pattern], 1) + unique_molecules.add(before + after) + + print(len(unique_molecules)) + +if __name__ == "__main__": + main() -- cgit v1.2.3