def run_program(commands, variables): i = 0 while i < len(commands): cmd = commands[i] parts = cmd.split() if parts[0] == "CLEAR": variables[parts[1]] = 0 elif parts[0] == "INCREASE": variables[parts[1]] += 1 elif parts[0] == "PRINT": print(variables[parts[1]], end=' ') elif parts[0] == "REPEAT": count_var = parts[1] count = variables[count_var] # Etsi silmukan sisältö inner = [] i += 1 depth = 1 while i < len(commands): line = commands[i] if line.startswith("REPEAT"): depth += 1 elif line == ")": depth -= 1 if depth == 0: break inner.append(line) i += 1 # Suorita sisäinen koodi 'count' kertaa for _ in range(count): run_program(inner, variables) i += 1 def main(): import sys # Lue koko ohjelma syötteestä code = sys.stdin.read().strip().splitlines() # Poista kommentit ja tyhjät rivit cleaned = [] for line in code: line = line.strip() if not line or line.startswith("#"): continue # sallitaan useampi komento samalla rivillä cleaned.extend(line.split(")")) cleaned = [cmd.strip() + ")" if i < len(cleaned) - 1 else cmd.strip() for i, cmd in enumerate(cleaned)] # Korjataan ylimääräiset splitit final = [] for line in code: line = line.strip() if not line or line.startswith("#"): continue for cmd in line.split(")"): cmd = cmd.strip() if cmd: if not cmd.endswith(")"): final.append(cmd) else: final.append(cmd) # Yksinkertaistetaan: käsitellään vain sanat final = [] tokens = [] for line in code: line = line.strip() if not line or line.startswith("#"): continue tokens.extend(line.split()) # Palautetaan rivitasoinen lista lines = [] i = 0 while i < len(tokens): token = tokens[i] if token == "REPEAT": lines.append("REPEAT " + tokens[i+1]) i += 3 # ohitetaan TIMES ( elif token == ")": lines.append(")") i += 1 else: lines.append(token + " " + tokens[i+1]) i += 2 # Suoritetaan variables = {chr(c): 0 for c in range(ord('A'), ord('Z') + 1)} run_program(lines, variables) if __name__ == "__main__": main()