Created
December 10, 2021 09:07
-
-
Save pil0u/a1be1c469513bf8880575a6004153622 to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 10
This file contains hidden or 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
closers = { | |
'[': ']', | |
'(': ')', | |
'<': '>', | |
'{': '}' | |
} | |
illegals = { | |
']': 0, | |
')': 0, | |
'>': 0, | |
'}': 0 | |
} | |
score_values = { | |
']': 2, | |
')': 1, | |
'>': 4, | |
'}': 3 | |
} | |
scores = [] | |
for line in input_: | |
should_be_next = [] | |
for char in line: | |
if char in closers.keys(): | |
should_be_next.insert(0, closers[char]) | |
else: | |
if char == should_be_next[0]: | |
del should_be_next[0] | |
else: | |
illegals[char] += 1 | |
should_be_next = [] | |
break | |
score = 0 | |
for val in should_be_next: | |
score = score * 5 + score_values[val] | |
if score != 0: | |
scores.append(score) | |
part_1 = illegals[')'] * 3 + illegals[']'] * 57 + illegals['}'] * 1197 + illegals['>'] * 25137 | |
part_2 = sorted(scores)[len(scores) // 2] | |
print(part_1, part_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment