-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10!.py
More file actions
50 lines (40 loc) · 1005 Bytes
/
day10!.py
File metadata and controls
50 lines (40 loc) · 1005 Bytes
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
lines = []
pairs = {'<': '>', '(': ')', '{': '}', '[': ']'}
corruptPrice = {'>':25137, ')':3, '}':1197, ']':57}
completePrice = {')':1, ']':2, '}':3, '>':4}
with open("day10full.txt") as f:
for l in f:
lines.append( l[:-1] )
def notCorrupt(line):
stack = []
for c in line:
if c in pairs.keys():
stack.append(c)
else:
if len(stack) == 0:
return False
latestStack = stack.pop()
if pairs[ latestStack ] != c:
return False
return True
def completeLine(line):
stack = []
for c in line:
if c in pairs.keys():
stack.append(c)
else:
stack.pop()
stack = list(map(lambda c: pairs[c], stack))
stack.reverse()
return stack
def completionCost(str):
score = 0
for c in str:
score = score * 5 + completePrice[c]
return score
incompleteLines = list(filter(notCorrupt, lines))
scores = []
for l in incompleteLines:
scores.append( completionCost(completeLine(l)) )
scores.sort()
print(scores[ len(scores)//2 ])