with open("input") as f: start_number = f.readline().strip() # start_number = "211" print("Start:", start_number) def generate_next_number(number): next_number = "" last_char = "" char_count = 0 for char in number: # initialize if not last_char: last_char = char char_count = 1 continue # same as last char: increase count if char == last_char: char_count += 1 continue # different than last char: append to next_number and initialize next_number = next_number + str(char_count) + last_char char_count = 1 last_char = char next_number = next_number + str(char_count) + last_char return next_number for i in range(40): print("Iteration:", i + 1) prev_number = start_number start_number = generate_next_number(start_number) # print(f"{prev_number} -> {start_number}") print("Solution:") print(len(start_number))