From 137880a94d619fc4d33e9203c119908b0dca5cb0 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 11 Dec 2024 10:33:54 +0100 Subject: [PATCH] add 2024/day11 --- 2024/11/main.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2024/11/main.py diff --git a/2024/11/main.py b/2024/11/main.py new file mode 100644 index 0000000..eeb3a88 --- /dev/null +++ b/2024/11/main.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + + +def readinput(): + with open("input", "r", encoding="utf-8") as file: + # initialize with a count of 1 for each number + stones = {int(n): 1 for n in file.readline().split()} + return stones + + +def blink(stone: str) -> list: + if stone == 0: + return [1] + + if len(str(stone)) % 2 == 0: + middle = len(str(stone)) // 2 + first = str(stone)[:middle] + second = str(stone)[middle:] + return [int(first), int(second)] + + return [int(stone) * 2024] + + +def solve(stones: dict) -> dict: + new_stones = {} + + for stone, count in stones.items(): + for num in blink(stone): + # count of each number, remember existing occurences + new_stones[num] = new_stones.get(num, 0) + count + + return new_stones + + +def main(): + # part 1 + 2 + for r in [25, 75]: + stones = readinput() + for _ in range(r): + stones = solve(stones) + print("Number of stones (%d blinks): %d" % (r, sum(stones.values()))) + + +if __name__ == "__main__": + main()