Cleanup
This commit is contained in:
52
10/10.py
52
10/10.py
@@ -1,16 +1,14 @@
|
|||||||
import sys
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
class InvalidChar(Exception):
|
class InvalidChar(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
PAIRS = {
|
OPEN = ["(", "[", "{", "<"]
|
||||||
")": "(",
|
CLOSE = [")", "]", "}", ">"]
|
||||||
"]": "[",
|
|
||||||
"}": "{",
|
CLOSING_FOR_OPEN = dict(zip(CLOSE, OPEN))
|
||||||
">": "<",
|
|
||||||
}
|
|
||||||
|
|
||||||
SCORE_ONE = {
|
SCORE_ONE = {
|
||||||
")": 3,
|
")": 3,
|
||||||
@@ -29,47 +27,51 @@ SCORE_TWO = {
|
|||||||
|
|
||||||
def part_one(lines: list[str]) -> int:
|
def part_one(lines: list[str]) -> int:
|
||||||
score = 0
|
score = 0
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
try:
|
try:
|
||||||
stack = []
|
stack = []
|
||||||
for char in list(line):
|
|
||||||
# Open backets
|
|
||||||
if char in PAIRS.values():
|
|
||||||
stack.append(char)
|
|
||||||
|
|
||||||
# Close brackets
|
for char in list(line):
|
||||||
elif char in PAIRS:
|
if char in CLOSING_FOR_OPEN.values(): # Open backets
|
||||||
|
stack.append(char)
|
||||||
|
elif char in CLOSING_FOR_OPEN: # Close brackets
|
||||||
top = stack.pop()
|
top = stack.pop()
|
||||||
if top != PAIRS[char]:
|
|
||||||
|
if top != CLOSING_FOR_OPEN[char]:
|
||||||
score += SCORE_ONE[char]
|
score += SCORE_ONE[char]
|
||||||
raise InvalidChar
|
raise InvalidChar
|
||||||
|
|
||||||
except InvalidChar:
|
except InvalidChar:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
||||||
def part_two(lines: list[str]) -> int:
|
def part_two(lines: list[str]) -> int:
|
||||||
scores = []
|
scores = []
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
score = 0
|
score = 0
|
||||||
try:
|
|
||||||
stack = [None]
|
|
||||||
for char in list(line):
|
|
||||||
# Open backets
|
|
||||||
if char in PAIRS.values():
|
|
||||||
stack.append(char)
|
|
||||||
|
|
||||||
# Close brackets
|
try:
|
||||||
elif char in PAIRS:
|
stack: list[Optional[str]] = [None]
|
||||||
|
|
||||||
|
for char in list(line):
|
||||||
|
if char in CLOSING_FOR_OPEN.values(): # Open backets
|
||||||
|
stack.append(char)
|
||||||
|
elif char in CLOSING_FOR_OPEN: # Close brackets
|
||||||
top = stack.pop()
|
top = stack.pop()
|
||||||
if top != PAIRS[char]:
|
if top != CLOSING_FOR_OPEN[char]:
|
||||||
|
# Ignore invalid lines
|
||||||
raise InvalidChar
|
raise InvalidChar
|
||||||
|
|
||||||
# Check not closed brackets
|
# Close unclosed pairs
|
||||||
while top := stack.pop():
|
while top := stack.pop():
|
||||||
score = score * 5 + SCORE_TWO[top]
|
score = score * 5 + SCORE_TWO[top]
|
||||||
|
|
||||||
scores.append(score)
|
scores.append(score)
|
||||||
|
|
||||||
except InvalidChar:
|
except InvalidChar:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -79,6 +81,8 @@ def part_two(lines: list[str]) -> int:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
|
||||||
lines = sys.stdin.readlines()
|
lines = sys.stdin.readlines()
|
||||||
|
|
||||||
print("Part one:", part_one(lines))
|
print("Part one:", part_one(lines))
|
||||||
|
|||||||
Reference in New Issue
Block a user