From 04bc876d392fd6ceb6e1828314aab71fe29b8131 Mon Sep 17 00:00:00 2001 From: kwout Date: Sat, 11 Feb 2023 12:49:39 -0500 Subject: [PATCH] solve 10b in 10a --- 2021/day10/10.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/2021/day10/10.py b/2021/day10/10.py index 27a63e4..41c85e2 100644 --- a/2021/day10/10.py +++ b/2021/day10/10.py @@ -2,21 +2,33 @@ lines = open("input.txt", 'r').read().splitlines() di = { - ')': ('(', 3), - ']': ('[', 57), - '}': ('{', 1197), - '>': ('<', 25137) + ('(', ')', 3, 1), + ('[', ']', 57, 2), + ('{', '}', 1197, 3), + ('<', '>', 25137, 4) } -score = 0 - +score1 = 0 +score2 = [] for l in lines: stack = [] + valid = True for c in l: if c == '(' or c == '[' or c == '{' or c == '<': stack.append(c) if c == ')' or c == ']' or c == '}' or c == '>': - if stack.pop() != di.get(c)[0]: - score += di.get(c)[1] + if stack.pop() != [x[0] for x in di if x[1] == c][0][0]: + score1 += [x for x in di if x[1] == c][0][2] + valid = False break + if valid: + if stack == []: + break + score = 0 + while stack != []: + cha = stack.pop() + score = score * 5 + [x for x in di if x[0] == cha][0][3] + score2.append(score) -print(score) \ No newline at end of file +print(score1) +score2.sort() +print(score2[(int)(len(score2)/2)]) \ No newline at end of file