-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
executable file
·46 lines (36 loc) · 986 Bytes
/
day10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# Day 10: Syntax Scoring
# https://adventofcode.com/2021/day/10
import sys
data = open("input.txt" if len(sys.argv) == 1 else sys.argv[1]).read().splitlines()
def check(line):
stack = []
corrupted = {")": 3, "]": 57, "}": 1197, ">": 25137}
completed = {")": 1, "]": 2, "}": 3, ">": 4}
for c in line:
if c == "<":
stack.append(">")
elif c == "(":
stack.append(")")
elif c == "[":
stack.append("]")
elif c == "{":
stack.append("}")
else:
d = stack.pop()
if c != d:
return corrupted[c], 0
score = 0
while stack:
score = score * 5 + completed[stack.pop()]
return 0, score
part1 = 0
part2 = []
for line in data:
corrupted, completed = check(line)
part1 += corrupted
if completed != 0:
part2.append(completed)
print(part1)
part2 = sorted(part2)
print(part2[len(part2) // 2])