Add solution for day 10
This commit is contained in:
1 parent
89ad5dccf3
commit
0235f5bc1a
4 files changed
+253
No files matched your search
Executable
+41
@@ -0,0 +1,41 @@
|
||||
#!/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")
|
||||
|
||||
Reference in new issue
Block a user