Created
December 10, 2021 05:25
-
-
Save albertein/2172bff21aeb5f14ef4e64a29a593c82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scores = { | |
')': 1, | |
']': 2, | |
'}': 3, | |
'>': 4 | |
} | |
expected = { | |
'(': ')', | |
'[': ']', | |
'{': '}', | |
'<': '>' | |
} | |
def score(line): | |
current_score = 0 | |
stack = [] | |
for char in line: | |
if char in expected.keys(): | |
stack.append(char) | |
else: | |
if char == expected[stack[-1]]: | |
stack.pop() | |
else: | |
return current_score | |
while stack: | |
current_score *= 5 | |
current_score += scores[expected[stack.pop()]] | |
return current_score | |
if __name__ == '__main__': | |
with open('input.txt') as data: | |
final_scores = [] | |
for line in data: | |
line = line.strip() | |
total_score = score(line) | |
if total_score: | |
final_scores.append(total_score) | |
print(sorted(final_scores)[len(final_scores) // 2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment