|
@ -9,7 +9,7 @@ import pathlib |
|
|
import sys |
|
|
import sys |
|
|
from typing import Dict, List, Tuple |
|
|
from typing import Dict, List, Tuple |
|
|
|
|
|
|
|
|
from collections import Counter |
|
|
|
|
|
|
|
|
from statistics import median |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -18,13 +18,14 @@ class Subsystem: |
|
|
open_chars = ['(', '[', '{', '<'] |
|
|
open_chars = ['(', '[', '{', '<'] |
|
|
close_chars = [')', ']', '}', '>'] |
|
|
close_chars = [')', ']', '}', '>'] |
|
|
|
|
|
|
|
|
def __init__(self, inputs): |
|
|
|
|
|
|
|
|
def __init__(self, inputs: List[str]) -> None: |
|
|
self.nav_data = inputs |
|
|
self.nav_data = inputs |
|
|
self.chunks, self.corrupted_chars = self.validate() |
|
|
|
|
|
|
|
|
self.validate() |
|
|
|
|
|
|
|
|
def validate(self) -> Tuple[Dict, List[str]]: |
|
|
|
|
|
|
|
|
def validate(self) -> None: |
|
|
chunks = {"corrupted": [], "incomplete": [], "complete": []} |
|
|
chunks = {"corrupted": [], "incomplete": [], "complete": []} |
|
|
corrupted_chars = [] |
|
|
corrupted_chars = [] |
|
|
|
|
|
incomplete_chunks = [] |
|
|
|
|
|
|
|
|
for line in self.nav_data: |
|
|
for line in self.nav_data: |
|
|
corrupted = False |
|
|
corrupted = False |
|
@ -47,15 +48,31 @@ class Subsystem: |
|
|
chunks["corrupted"].append(line) |
|
|
chunks["corrupted"].append(line) |
|
|
elif len(opens) != 0: |
|
|
elif len(opens) != 0: |
|
|
chunks["incomplete"].append(line) |
|
|
chunks["incomplete"].append(line) |
|
|
|
|
|
incomplete_chunks.append(opens) |
|
|
else: |
|
|
else: |
|
|
chunks["complete"].append(line) |
|
|
chunks["complete"].append(line) |
|
|
|
|
|
|
|
|
return chunks, corrupted_chars |
|
|
|
|
|
|
|
|
self.chunks = chunks |
|
|
|
|
|
self.corrupted_chars = corrupted_chars |
|
|
|
|
|
self.incomplete_chunks = incomplete_chunks |
|
|
|
|
|
|
|
|
def score_corrupted(self) -> int: |
|
|
def score_corrupted(self) -> int: |
|
|
scores = {")": 3, "]": 57, "}": 1197, ">": 25137,} |
|
|
scores = {")": 3, "]": 57, "}": 1197, ">": 25137,} |
|
|
return sum([scores[char] for char in self.corrupted_chars]) |
|
|
return sum([scores[char] for char in self.corrupted_chars]) |
|
|
|
|
|
|
|
|
|
|
|
def score_incomplete(self) -> int: |
|
|
|
|
|
scores = {")": 1, "]": 2, "}": 3, ">": 4,} |
|
|
|
|
|
total_scores = [] |
|
|
|
|
|
for chunk in self.incomplete_chunks: |
|
|
|
|
|
total_score = 0 |
|
|
|
|
|
for char in reversed(chunk): |
|
|
|
|
|
close_char = self.close_chars[self.open_chars.index(char)] |
|
|
|
|
|
total_score = total_score * 5 + scores[close_char] |
|
|
|
|
|
total_scores.append(total_score) |
|
|
|
|
|
|
|
|
|
|
|
return median(total_scores) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def part1(inputs: List[int]) -> int: |
|
|
def part1(inputs: List[int]) -> int: |
|
|
s = Subsystem(inputs) |
|
|
s = Subsystem(inputs) |
|
@ -63,7 +80,8 @@ def part1(inputs: List[int]) -> int: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def part2(inputs: List[int]) -> int: |
|
|
def part2(inputs: List[int]) -> int: |
|
|
return False |
|
|
|
|
|
|
|
|
s = Subsystem(inputs) |
|
|
|
|
|
return s.score_incomplete() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse(inputs: str) -> List[int]: |
|
|
def parse(inputs: str) -> List[int]: |
|
|