spacepaste

  1.  
  2. def main():
  3. with open("input.txt") as f:
  4. stream = list(f.read())
  5. valid_characters = ['{', '}']
  6. filtered_stream = []
  7. state = True
  8. for index, char in enumerate(stream):
  9. if char == '<' and isnt_not(index, stream):
  10. state = False
  11. if state == True and char in valid_characters:
  12. filtered_stream.append(char)
  13. if char == '>' and isnt_not(index, stream):
  14. state = True
  15. print(get_score(filtered_stream))
  16. def isnt_not(index, stream):
  17. '''Check if the previous characters cancel the current character'''
  18. if stream[index - 1] != '!':
  19. return True
  20. else:
  21. i = 0
  22. while stream[(index - 1) - i] == '!':
  23. i += 1
  24. return i % 2 == 0
  25. def get_score(filtered_stream):
  26. '''return total score from list of brackets'''
  27. score, m = 0, 0
  28. for char in filtered_stream:
  29. if char == '{':
  30. m += 1
  31. score += m
  32. elif char == '}':
  33. m -= 1
  34. return score
  35. if __name__ == '__main__':
  36. main()
  37.