Day 10
This commit is contained in:
parent
a1a099c712
commit
24ee5520bf
37
2021/day10.py
Normal file
37
2021/day10.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import sys
|
||||
|
||||
|
||||
CHARMAP = {"]": "[", ")": "(", "}": "{", ">": "<"}
|
||||
SCOREMAP = {")": 3, "]": 57, "}": 1197, ">": 25137}
|
||||
SCOREMAP2 = {"(": 1, "[": 2, "{": 3, "<": 4}
|
||||
|
||||
|
||||
def main():
|
||||
lines = [line.rstrip() for line in sys.stdin]
|
||||
error_score = 0
|
||||
comp_scores = []
|
||||
for line in lines:
|
||||
corrupted = None
|
||||
state = []
|
||||
for char in line:
|
||||
if char in CHARMAP:
|
||||
if state and state[-1] == CHARMAP[char]:
|
||||
state.pop()
|
||||
else:
|
||||
corrupted = char
|
||||
break
|
||||
else:
|
||||
state.append(char)
|
||||
if corrupted:
|
||||
error_score += SCOREMAP[corrupted]
|
||||
else:
|
||||
cs = 0
|
||||
for char in reversed(state):
|
||||
cs = cs * 5 + SCOREMAP2[char]
|
||||
comp_scores.append(cs)
|
||||
print(error_score)
|
||||
print(list(sorted(comp_scores))[len(comp_scores) // 2])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue