from collections import Counter def look_and_say(n: str) -> str: """ for every consecutive chunk of characters, count the number of characters, concatenate the chunk length and the character """ res = [] char = n[0] count = 1 for c in n[1:]: if c == char: count += 1 else: res.append(str(count) + char) char = c count = 1 res.append(str(count) + char) return ''.join(res) def part1(n: str) -> str: for i in range(40): n = look_and_say(n) return len(n) def part2(n: str) -> str: for i in range(50): n = look_and_say(n) return len(n) if __name__ == '__main__': inp = '1113222113' print(part1(inp)) print(part2(inp))