Better Stack
This commit is contained in:
10
10/10.py
10
10/10.py
@@ -1,6 +1,3 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidChar(Exception):
|
class InvalidChar(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -55,20 +52,21 @@ def part_two(lines: list[str]) -> int:
|
|||||||
score = 0
|
score = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stack: list[Optional[str]] = [None]
|
stack: list[str] = []
|
||||||
|
|
||||||
for char in list(line):
|
for char in list(line):
|
||||||
if char in CLOSING_FOR_OPEN.values(): # Open backets
|
if char in CLOSING_FOR_OPEN.values(): # Open backets
|
||||||
stack.append(char)
|
stack.append(char)
|
||||||
elif char in CLOSING_FOR_OPEN: # Close brackets
|
elif char in CLOSING_FOR_OPEN: # Close brackets
|
||||||
top = stack.pop()
|
top = stack.pop()
|
||||||
|
|
||||||
if top != CLOSING_FOR_OPEN[char]:
|
if top != CLOSING_FOR_OPEN[char]:
|
||||||
# Ignore invalid lines
|
# Ignore invalid lines
|
||||||
raise InvalidChar
|
raise InvalidChar
|
||||||
|
|
||||||
# Close unclosed pairs
|
# Close unclosed pairs
|
||||||
while top := stack.pop():
|
while stack:
|
||||||
score = score * 5 + SCORE_TWO[top]
|
score = score * 5 + SCORE_TWO[stack.pop()]
|
||||||
|
|
||||||
scores.append(score)
|
scores.append(score)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user