Skip to content

Instantly share code, notes, and snippets.

@spielkind
Created December 14, 2021 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spielkind/f4e2cde5db18f05a5bfadd2a407139a9 to your computer and use it in GitHub Desktop.
Save spielkind/f4e2cde5db18f05a5bfadd2a407139a9 to your computer and use it in GitHub Desktop.
#!/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