Sparrow1029

AoC 2017: Day 9 (Parts 1 & 2)

Dec 22nd, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Advent of Code 2017 -- Day 9: Stream Processing -- Alex Ray
  5. # https://adventofcode.com/2017/day/9
  6.  
  7. def recording(stream):
  8.  
  9.     global total_score
  10.     global group_depth
  11.     global collecting_garbage
  12.  
  13.     print("Recording...")
  14.  
  15.     while True:
  16.         current = stream.pop(0)
  17.         print(current)
  18.         if current == "!":
  19.             x = stream.pop(0)
  20.             print("cancelled: %s" % x)
  21.             # stream.pop(0)
  22.             continue
  23.         elif current == "<":
  24.             collecting_garbage = True
  25.             return
  26.         elif current == "{":
  27.             group_depth += 1
  28.             continue
  29.         elif current == "}":
  30.             total_score += group_depth
  31.             group_depth -= 1
  32.             continue
  33.         else:
  34.             pass
  35.  
  36. def trashing(stream):
  37.  
  38.     global collecting_garbage
  39.     global piecesOfTrash
  40.  
  41.     print("Collecting garbage...")
  42.  
  43.     while True:
  44.         char = stream.pop(0)
  45.         print(char)
  46.         if char == "!":
  47.             x = stream.pop(0)
  48.             print("cancelled: %s" % x)
  49.             # stream.pop(0)
  50.             continue
  51.         elif char == ">":
  52.             collecting_garbage = False
  53.             return
  54.         else:
  55.             piecesOfTrash += 1  # <--- PART TWO
  56.  
  57.  
  58. total_score = 0
  59. group_depth = 0
  60. collecting_garbage = False
  61. piecesOfTrash = 0
  62.  
  63. def parsing(string):
  64.     while string:
  65.         while not collecting_garbage:
  66.             recording(string)
  67.         trashing(string)
  68.  
  69. with open('day9input.txt', 'r') as f:
  70.     string = list(f.read())
  71.  
  72. try:
  73.     parsing(string)
  74. except IndexError:
  75.     print("Reached end of input.")
  76.  
  77. print("Total score: %r" % total_score)
  78. print("Trash collected: %r " % piecesOfTrash)
Advertisement
Add Comment
Please, Sign In to add comment