-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
37 lines (29 loc) · 837 Bytes
/
processing.py
File metadata and controls
37 lines (29 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
""" Advent of Code 2017. Day 9: Stream Processing """
def parse(stream):
level = 0
group_score = 0
garbage_size = 0
i = 0
is_garbage = False
while i < len(stream):
if stream[i] == "!":
# Skip next char
i += 1
elif stream[i] == "<" and not is_garbage:
is_garbage = True
elif stream[i] == ">":
is_garbage = False
elif stream[i] == "{" and not is_garbage:
level += 1
group_score += level
elif stream[i] == "}" and not is_garbage:
level -= 1
elif is_garbage:
garbage_size += 1
i += 1
return group_score, garbage_size
with open("input.txt") as f:
stream = f.read()
score, garbage = parse(stream)
print("Part 1:\t", score)
print("Part 2:\t", garbage)