from functools import lru_cache data = open(0).read().strip() part_1 = False if part_1: repeats = 40 else: repeats = 50 @lru_cache def look_and_say(data: str) -> str: r = "" i = 0 while i < len(data): count = 0 c = data[i] while i < len(data) and data[i] == c: count += 1 i += 1 r += f"{count}{c}" return r CHUNK_SIZE = 10000 for _ in range(repeats): ndata = "" lo, up = 0, CHUNK_SIZE while up < len(data): while up < len(data) and data[up - 1] == data[up]: up += 1 ndata += look_and_say(data[lo:up]) lo, up = up, up + CHUNK_SIZE ndata += look_and_say(data[lo:up]) data = ndata print(len(data))