From 911e0aa6a21b9372d424758ec0ae10c111e47584 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 29 Dec 2024 16:10:28 -0500 Subject: [PATCH] 2018: d05: ex1: add solution --- 2018/d05/ex1/ex1.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 2018/d05/ex1/ex1.py diff --git a/2018/d05/ex1/ex1.py b/2018/d05/ex1/ex1.py new file mode 100755 index 0000000..b93dfcb --- /dev/null +++ b/2018/d05/ex1/ex1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import sys + + +def solve(input: str) -> int: + def reduce_polymer(polymer: str) -> str: + for i in range(26): + lower, upper = chr(ord("a") + i), chr(ord("A") + i) + polymer = polymer.replace(lower + upper, "") + polymer = polymer.replace(upper + lower, "") + return polymer + + polymer = input.strip() + while True: + reduced = reduce_polymer(polymer) + if reduced == polymer: + return len(polymer) + polymer = reduced + assert False # Sanity check + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()