-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10_2.py
72 lines (61 loc) · 1.92 KB
/
day10_2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def inp():
tmp = []
for line in open("input_d10.txt", "rt"):
line = line.replace("\n", "")
tmp.append(line)
return tmp
def check_syntax(line):
last_opened = [line[0]]
for char in line[1:]:
if char in "[{<(":
last_opened.append(char)
elif char in "]}>)":
if (last_opened[-1] == "[" and char == "]") or (last_opened[-1] == "{" and char == "}") or (last_opened[-1] == "(" and char == ")") or (last_opened[-1] == "<" and char == ">"):
del last_opened[-1]
continue
else:
return False
return True
def find_missing(line):
last_opened = [line[0]]
for char in line[1:]:
if char in "[{<(":
last_opened.append(char)
elif char in "]}>)":
if (last_opened[-1] == "[" and char == "]") or (last_opened[-1] == "{" and char == "}") or (last_opened[-1] == "(" and char == ")") or (last_opened[-1] == "<" and char == ">"):
del last_opened[-1]
continue
txt = ""
for i in range(len(last_opened)-1, -1, -1):
if last_opened[i] == "[":
txt += "]"
elif last_opened[i] == "{":
txt += "}"
elif last_opened[i] == "<":
txt += ">"
elif last_opened[i] == "(":
txt += ")"
return txt
def sort(arr):
for i in range(len(arr)-1):
for j in range(i, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
return arr
lines = inp()
inds = []
for i in range(len(lines)):
if check_syntax(lines[i]) == False:
inds.insert(0, i)
for ind in inds:
del lines[ind]
scores = []
points = { ")":1, "]":2, "}":3, ">":4}
for line in lines:
missing = find_missing(line)
tmp = 0
for char in missing:
tmp = 5*tmp + points[char]
scores.append(tmp)
scores = sort(scores)
print(scores[len(scores)//2])