def main(): with open("input.txt") as f: stream = list(f.read()) garbage_stream = [] state = False for index, char in enumerate(stream): if char == '>' and isnt_not(index, stream): state = False if state == True and char != '!' and isnt_not(index, stream): garbage_stream.append(char) if char == '<' and isnt_not(index, stream): state = True print(len(garbage_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 if __name__ == '__main__': main()