-
- def main():
- with open("input.txt") as f:
- stream = list(f.read())
-
- valid_characters = ['{', '}']
- filtered_stream = []
- state = True
-
- for index, char in enumerate(stream):
- if char == '<' and isnt_not(index, stream):
- state = False
-
- if state == True and char in valid_characters:
- filtered_stream.append(char)
-
- if char == '>' and isnt_not(index, stream):
- state = True
-
- print(get_score(filtered_stream))
-
-
- def isnt_not(index, stream):
- '''Check if the previous characters cancel the current character'''
- if stream[index - 1] != '!':
- return True
- else:
- i = 0
- while stream[(index - 1) - i] == '!':
- i += 1
- return i % 2 == 0
-
-
- def get_score(filtered_stream):
- '''return total score from list of brackets'''
- score, m = 0, 0
- for char in filtered_stream:
- if char == '{':
- m += 1
- score += m
- elif char == '}':
- m -= 1
- return score
-
-
- if __name__ == '__main__':
- main()
-