+from dataclasses import dataclass\r
+from collections import defaultdict\r
+from typing import List, DefaultDict, TextIO\r
+\r
+\r
+@dataclass\r
+class Problem:\r
+ data: DefaultDict[int, int]\r
+\r
+ @staticmethod\r
+ def calc(value: int) -> List[int]:\r
+ if value == 0:\r
+ result = [1]\r
+ elif len(str(value)) % 2 == 0:\r
+ digits = str(value)\r
+ mid = len(digits) // 2\r
+ result = [int(digits[:mid]), int(digits[mid:])]\r
+ else:\r
+ result = [value * 2024]\r
+ return result\r
+\r
+ def run(self, count: int, debug: bool = False) -> int:\r
+ data = self.data\r
+ if debug:\r
+ print(data)\r
+ for _ in range(count):\r
+ newdata: DefaultDict[int, int] = defaultdict(int)\r
+ for value, count in data.items():\r
+ for newval in Problem.calc(value):\r
+ newdata[newval] += count\r
+ if debug:\r
+ print(newdata)\r
+ data = newdata\r
+ return sum(data.values())\r
+\r
+ @staticmethod\r
+ def from_stream(stream: TextIO) -> 'Problem':\r
+ line = next(stream).strip() # just one line\r
+ data = defaultdict(int, {int(x): 1 for x in line.split(' ')})\r
+ return Problem(data)\r