-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.py
51 lines (44 loc) · 1.41 KB
/
day_10.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
47
48
49
50
51
def parse():
with open("input.txt", "r") as fp:
return [i.strip('\n') for i in fp.readlines()]
class Node():
def __init__(self, val, nxt=None):
self.val = val
self.nxt = nxt
class Stack():
def __init__(self, val=None):
self.head = Node(val) if val else None
def push(self, val):
self.head = Node(val, self.head)
def peak(self):
return self.head.val if self.head else None
def pop(self):
if self.head:
val = self.head.val
self.head = self.head.nxt if self.head.nxt else None
return val
def process(lines):
corruption = 0
incomplete = []
for line in lines:
stack = Stack()
corrupted = False
for bracket in line:
if bracket in "([{<":
stack.push(")]}>"["([{<".index(bracket)])
else:
if bracket == stack.peak():
stack.pop()
else:
corruption += {')': 3, ']': 57, '}': 1197, '>': 25137}[bracket]
corrupted = True
break
if not corrupted:
tmp = 0
while (stack.peak()):
tmp *= 5
tmp += [')', ']', '}', '>'].index(stack.pop()) + 1
incomplete.append(tmp)
incomplete.sort()
return (corruption, incomplete[int(len(incomplete) / 2)])
print(process(parse()))