-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10b.py
36 lines (28 loc) · 1002 Bytes
/
day10b.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
from functools import reduce
class Stack:
def __init__(self):
self.stack = []
self.braces = { ')': '(', ']': '[', '}': '{', '>': '<' }
self.cost = "([{<"
self.failed = None
def push(self, char):
expect = self.braces.get(char, None)
if expect is None:
self.stack.append(char)
elif self.stack[-1] == expect:
self.stack.pop()
else:
self.failed = True
def complete_score(self):
self.stack.reverse()
score = (self.cost.index(char) + 1 for char in self.stack)
return reduce(lambda acc, scr: acc * 5 + scr, score, 0)
def complete_score(line):
stack = Stack()
for char in line:
if not stack.failed:
stack.push(char)
return 0 if stack.failed else stack.complete_score()
with open("day10.txt") as file:
scores = sorted(s for s in (complete_score(line.strip()) for line in file.readlines()) if s > 0)
print scores[len(scores)/2]