Watch
1
0
Fork
You've already forked advent-of-code
0
advent-of-code/2017/day09.py
2026-07-30 22:09:35 -04:00

47 lines
900 B
Python

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)