def clean(text: str) -> tuple[str, int]: out = "" removed = 0 i = 0 garbage = False while i < len(text): c = text[i] if c == "!": i += 2 continue if garbage and c == ">": garbage = False elif garbage: removed += 1 elif c == "<": garbage = True else: out += c i += 1 assert not garbage, "All garbage must be closed" return out, removed def score(text: str) -> int: depth = 0 points = 0 for c in text: if c == "{": depth += 1 if c == "}": points += depth depth -= 1 assert depth == 0, "All groups must be closed" return points with open("input/day09.txt") as f: puzzle_input = f.read() groups, garbage = clean(puzzle_input) print(score(groups)) print(garbage)