-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution10-2.py
More file actions
55 lines (46 loc) · 1.22 KB
/
Copy pathsolution10-2.py
File metadata and controls
55 lines (46 loc) · 1.22 KB
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
52
53
54
55
def main():
file = open('input10.txt', 'r')
file = file.readlines()
illegalscore = 0
incompletescore = []
openclose = {
'{': '}',
'[': ']',
'(': ')',
'<': '>'
}
closeopen = {value : key for (key, value) in openclose.items()}
illegalvalue = {
'}': 1197,
']': 57,
')': 3,
'>': 25137
}
incompletevalue = {
'}': 3,
']': 2,
')': 1,
'>': 4
}
for line in file:
array = []
isIncomplete = True
for x in line:
if x in openclose:
array.append(x)
elif x in closeopen:
opener = array.pop()
if opener != closeopen[x]:
illegalscore += illegalvalue[x]
isIncomplete = False
break
if isIncomplete:
incompletecost = 0
array.reverse()
for x in array:
incompletecost = (5*incompletecost) + incompletevalue[openclose[x]]
incompletescore.append(incompletecost)
incompletescore.sort()
print(illegalscore)
print(incompletescore[len(incompletescore)/2])
main()