10.py
1 from lib import * 2 3 input = read_input(2021, 10) 4 5 lines = input.splitlines() 6 7 out = 0 8 for line in lines: 9 stack = [] 10 11 for c in line: 12 if c in "([{<": 13 stack.append(c) 14 15 else: 16 x = stack.pop() 17 18 if x + c not in ["()", "[]", "{}", "<>"]: 19 out += {")": 3, "]": 57, "}": 1197, ">": 25137}[c] 20 21 break 22 23 else: 24 out += 0 25 26 27 print(out) 28 29 30 out = [] 31 for line in lines: 32 stack = [] 33 34 for c in line: 35 if c in "([{<": 36 stack.append(c) 37 38 else: 39 x = stack.pop() 40 41 if x + c not in ["()", "[]", "{}", "<>"]: 42 break 43 44 else: 45 s = 0 46 47 for c in reversed(stack): 48 s = s * 5 + " ([{<".index(c) 49 50 out.append(s) 51 52 out.sort() 53 print(out[len(out) // 2])