openers = ['(', '[', '{', '<'] closers = [')', ']', '}', '>'] points = [3, 57, 1197, 25137] score = 0 with open('data', 'r') as fp: # NOTE(dev): We assume all lines are the same length for line in fp: stack = [] for ch in line.strip(): line_score = 0 if(ch in openers): stack.append(ch) elif(len(stack) == 0): try: i = closers.index(ch) line_score += points[closers.index(ch)] except ValueError: # not a closer pass else: try: i = closers.index(ch) last = stack[-1] stack.pop() if(last != openers[i]): line_score += points[i] score += line_score print(last, '!=', openers[i]) break except ValueError: # not an opener or closer! pass score += line_score print(score)