def part1(text): return score(clean(text)) def clean(text: str) -> str: out = "" i = 0 garbage = False while i < len(text): c = text[i] if c == "!": i += 2 continue if garbage and c == ">": garbage = False elif garbage: pass elif c == "<": garbage = True else: out += c i += 1 assert not garbage, "All garbage must be closed" return out 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() print(part1(puzzle_input))