Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to Day 9: Stream Processing - Part 2
#aoc_d9.py
score = 0
inp = open("input_d9.txt","r").read()
#"{{<ab>},{<ab>},{<ab>},{<ab>}}"
#"{{{},{},{{}}}}"
#"{{<a!>},{<a!>},{<a!>},{<ab>}}"
#"{{<!!>},{<!!>},{<!!>},{<!!>}}"
level = 0
garbage = False
ignore = False
garbage_count = 0
#print inp
for c in inp:
print "c:",c, "g:", garbage, "i:", ignore, "l:",level, "s:",score,"ct:",garbage_count
if c == "{" and not garbage:
#opening a group
level += 1
if c == "}" and not garbage:
#closing a group
score += level
level -= 1
if c == "<" and not ignore and not garbage:
garbage = True
continue
if c == ">" and not ignore:
garbage = False
if c == "!" and garbage and not ignore:
ignore = True
continue
if garbage and not ignore:
garbage_count += 1
if ignore:
ignore = False
print "c:",c, "g:", garbage, "i:", ignore, "l:",level, "s:",score,"ct:",garbage_count
#print "----------------"
print score, garbage_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment