-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.py
40 lines (34 loc) · 977 Bytes
/
day09.py
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
38
39
40
'''Advent of Code Day 9'''
# Part One and Two
nest_level = 0
total_score = 0
garbage = False
garbage_count = 0
with open('day09input.txt') as f:
# Read characters one at a time
char = f.read(1)
while char:
# Check if in a garbage state
if garbage:
# Skip over the escaped characters
if char == '!':
f.read(1)
char = f.read(1)
continue
elif char == '>':
garbage = False # Garbage close character
else:
# Count the garbage for part two
garbage_count += 1
char = f.read(1)
continue
# Track nest level
if char == '{':
nest_level += 1
total_score += nest_level
elif char == '}' and nest_level > 0:
nest_level -= 1
elif char == '<':
garbage = True
char = f.read(1)
print(total_score)