Created
December 19, 2021 20:29
-
-
Save maksverver/0530ee20e0845b2fe459cbc09c3feacd 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
import sys | |
opening = '([{<' | |
closing = ')]}>' | |
values = [3, 57, 1197, 25137] | |
answer1 = 0 | |
scores2 = [] | |
stack = [] | |
while ch := sys.stdin.read(1): | |
if ch == '\n': | |
# For part 2 only -- alternative: stack = [] | |
if stack: | |
# incomplete string found | |
score = 0 | |
while stack: | |
score *= 5 | |
score += stack.pop() + 1 | |
scores2.append(score) | |
elif ch in opening: | |
i = opening.index(ch) | |
stack.append(i) | |
elif ch in closing: | |
i = closing.index(ch) | |
if stack.pop() != i: | |
stack = [] | |
answer1 += values[i] | |
while ch != '\n': | |
ch = sys.stdin.read(1) | |
assert ch | |
else: | |
assert False | |
print(answer1) | |
scores2.sort() | |
while len(scores2) > 2: | |
scores2.pop() | |
scores2.pop(0) | |
print(scores2[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment