Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Advent of Code 2017 -- Day 9: Stream Processing -- Alex Ray
- # https://adventofcode.com/2017/day/9
- def recording(stream):
- global total_score
- global group_depth
- global collecting_garbage
- print("Recording...")
- while True:
- current = stream.pop(0)
- print(current)
- if current == "!":
- x = stream.pop(0)
- print("cancelled: %s" % x)
- # stream.pop(0)
- continue
- elif current == "<":
- collecting_garbage = True
- return
- elif current == "{":
- group_depth += 1
- continue
- elif current == "}":
- total_score += group_depth
- group_depth -= 1
- continue
- else:
- pass
- def trashing(stream):
- global collecting_garbage
- global piecesOfTrash
- print("Collecting garbage...")
- while True:
- char = stream.pop(0)
- print(char)
- if char == "!":
- x = stream.pop(0)
- print("cancelled: %s" % x)
- # stream.pop(0)
- continue
- elif char == ">":
- collecting_garbage = False
- return
- else:
- piecesOfTrash += 1 # <--- PART TWO
- total_score = 0
- group_depth = 0
- collecting_garbage = False
- piecesOfTrash = 0
- def parsing(string):
- while string:
- while not collecting_garbage:
- recording(string)
- trashing(string)
- with open('day9input.txt', 'r') as f:
- string = list(f.read())
- try:
- parsing(string)
- except IndexError:
- print("Reached end of input.")
- print("Total score: %r" % total_score)
- print("Trash collected: %r " % piecesOfTrash)
Advertisement
Add Comment
Please, Sign In to add comment