From bf55c705754eae73a1863aca6a9a7d4f06ca6df0 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 11 Dec 2024 09:16:17 -0500 Subject: [PATCH] 2024: d11: ex2: add solution --- 2024/d11/ex2/ex2.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 2024/d11/ex2/ex2.py diff --git a/2024/d11/ex2/ex2.py b/2024/d11/ex2/ex2.py new file mode 100755 index 0000000..e9c5900 --- /dev/null +++ b/2024/d11/ex2/ex2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import sys +from collections import Counter + + +def solve(input: str) -> int: + def parse(input: str) -> Counter[int]: + return Counter(int(n) for n in input.split()) + + def blink(stones: Counter[int]) -> Counter[int]: + res: Counter[int] = Counter() + for n, count in stones.items(): + if n == 0: + res[1] += count + elif len(str(n)) % 2 == 0: + s = str(n) + lhs, rhs = s[: len(s) // 2], s[len(s) // 2 :] + res[int(lhs)] += count + res[int(rhs)] += count + else: + res[n * 2024] += count + return res + + stones = parse(input) + for _ in range(75): + stones = blink(stones) + return sum(stones.values()) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()