X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=10_2.py;fp=10_2.py;h=c785b85dfdf59fe25a29aa72deb6f56001a944da;hb=682e43490516ca88171f8163679f2be6b1366fa6;hp=0000000000000000000000000000000000000000;hpb=480ab6a21d489c0a11f89321502090ddc2ecd2d6;p=max%2Fadvent_of_code_2021.git diff --git a/10_2.py b/10_2.py new file mode 100644 index 0000000..c785b85 --- /dev/null +++ b/10_2.py @@ -0,0 +1,44 @@ +"""Find and score bothersome broken brackets and fix the damn things""" +import pprint + +parens = { + ")": "(", + "]": "[", + "}": "{", + ">": "<" +} + +parens_rev = {v: k for k, v in parens.items()} + +scores = { + ")": 1, + "]": 2, + "}": 3, + ">": 4, +} + +line_scores = [] +with open("10_input.txt") as f: + for line in f: + stack = [] + broken = False + for char in line.strip(): + if char in "(<[{": + stack.append(char) + else: + if len(stack) == 0 or parens[char] != stack.pop(): + broken = True + break + + if not broken: + line_score = 0 + for i in range(len(stack) - 1, -1, -1): + open_char = stack[i] + close_char = parens_rev[stack[i]] + line_score *= 5 + line_score += scores[parens_rev[stack[i]]] + line_scores.append(line_score) + +line_scores.sort() +middle_i = (len(line_scores) - 1)//2 +print(line_scores[middle_i])