spacepaste

  1.  
  2. def main():
  3. with open("input.txt") as f:
  4. stream = list(f.read())
  5. garbage_stream = []
  6. state = False
  7. for index, char in enumerate(stream):
  8. if char == '>' and isnt_not(index, stream):
  9. state = False
  10. if state == True and char != '!' and isnt_not(index, stream):
  11. garbage_stream.append(char)
  12. if char == '<' and isnt_not(index, stream):
  13. state = True
  14. print(len(garbage_stream))
  15. def isnt_not(index, stream):
  16. '''Check if the previous characters cancel the current character'''
  17. if stream[index - 1] != '!':
  18. return True
  19. else:
  20. i = 0
  21. while stream[(index - 1) - i] == '!':
  22. i += 1
  23. return i % 2 == 0
  24. if __name__ == '__main__':
  25. main()
  26.