Created
December 14, 2021 16:23
-
-
Save spielkind/f4e2cde5db18f05a5bfadd2a407139a9 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/python | |
with open('day10.txt') as f: | |
lines = [list(line.strip()) for line in f] | |
bracketpair = {'(': ')', '[': ']', '{': '}', '<': '>'} | |
points = {')': 3, ']': 57, '}': 1197, '>': 25137} | |
scores = {')': 1, ']': 2, '}': 3, '>': 4} | |
illegal = 0 | |
totalscores = [] | |
for line in lines: | |
unclosed = [] | |
corrupted = False | |
for bracket in line: | |
if bracket in bracketpair.keys(): | |
unclosed.append(bracket) | |
else: | |
if bracket == bracketpair[unclosed[-1]]: | |
unclosed.pop() | |
else: | |
illegal += points[bracket] | |
corrupted = True | |
break | |
if not corrupted and len(unclosed) > 0: | |
missingbrackets = [ bracketpair[bracket] for bracket in reversed(unclosed) ] | |
totalscore = 0 | |
for bracket in missingbrackets: | |
totalscore *= 5 | |
totalscore += scores[bracket] | |
totalscores.append(totalscore) | |
print('Part One:', illegal) | |
print('Part Two:', sorted(totalscores)[len(totalscores)//2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment