You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
1.0 KiB

#!/usr/bin/env python3
import sys
import statistics
def corr(line):
st = []
for b in line:
if b in match.keys(): st.append(b)
if b in error.keys():
if not (st.pop(), b) in match.items(): return error[b]
return 0
def inc(line):
st, score = [], 0
for b in line:
if b in match.keys(): st.append(b)
if b in comp.keys(): st.pop()
miss = [match[b] for b in st]
for b in reversed(miss):
score *= 5
score += comp[b]
return score
if __name__ == '__main__':
inp = [list(i) for i in open(sys.argv[1]).read().strip('\n\n').split('\n')]
match = {'(': ')', '{': '}', '[': ']', '<': '>'}
# challenge 1
error = {')': 3, ']': 57, '}': 1197, '>': 25137}
res1 = str(sum([corr(line) for line in inp]))
print("challenge 1:" +"\n" + res1 + "\n")
# challenge 2
comp = {')': 1, ']': 2, '}': 3, '>': 4}
res2 = str(statistics.median([inc(line) for line in inp if corr(line) == 0]))
print("challenge 2:" +"\n" + res2 + "\n")