-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10puzzle2.py
60 lines (52 loc) · 1.47 KB
/
Day10puzzle2.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
data = open("E:\CodingStuff\AdventOfCode\Day10imput.txt")
data = data.readlines()
for row in range(len(data)):
data[row] = data[row].strip("\n")
openchars = ["<","{","[","("]
closechars = [">","}","]",")"]
scoredict = {">": 4, "}":3, "]": 2, ")":1}
class line:
def __init__(self, line):
self.line = line
def findissue(self):
stack = []
for char in self.line:
if char in openchars:
stack.append(char)
elif(openchars[closechars.index(char)]==stack[-1]):
del stack[-1]
else:
return True
return False
def findcompletionstring(self):
self.completionstring=""
stack = []
for char in self.line:
if char in openchars:
stack.append(char)
elif(openchars[closechars.index(char)]==stack[-1]):
del stack[-1]
for char in stack:
self.completionstring+=closechars[openchars.index(char)]
return self.completionstring
linelist = []
x=0
for ln in data:
linelist.append(line(ln))
if(linelist[x].findissue()):
del linelist[-1]
x-=1
x+=1
scorelist = []
i=0
print(len(linelist))
for ln in linelist:
scorelist.append(0)
for char in reversed(ln.findcompletionstring()):
scorelist[i]*=5
scorelist[i]+=scoredict[char]
i+=1
scorelist.sort()
print(len(scorelist)//2)
print(scorelist)
print(scorelist[len(scorelist)//2])