-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.1.py
39 lines (34 loc) · 856 Bytes
/
day10.1.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
import sys
scoremap = {
")": 3,
"]": 57,
"}": 1197,
">": 25137,
}
closechars = [")", "]", "}", ">",]
openchars = ["(", "[", "{", "<",]
def get_illchar_score(fd):
score = 0
for line in fd:
stack = []
for ch in line:
if ch == "\n":
continue
if ch in openchars:
stack.append(ch)
continue
topidx = openchars.index(stack[-1])
if closechars[topidx] == ch:
stack.pop(-1)
continue
score += scoremap[ch] # illegal char found!
break
return score
def main(fd):
print(get_illchar_score(fd))
if __name__ == '__main__':
try:
with open(sys.argv[1]) as f:
main(f)
except IndexError:
print(f"python {sys.argv[0]} <input_file.txt>")