All checks were successful
continuous-integration/drone/push Build is passing
64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
|
|
o_chars = ['(', '[', '{', '<']
|
|
c_chars = [')', ']', '}', '>']
|
|
missing_val = [3, 57, 1197, 25137]
|
|
corrupted_val = [1, 2, 3, 4]
|
|
|
|
|
|
def part_1(input):
|
|
result = 0
|
|
wrong_chars = []
|
|
for line in input:
|
|
line = line.rstrip()
|
|
seen = []
|
|
for c in line:
|
|
if c in o_chars:
|
|
seen.append(c)
|
|
elif o_chars.index(seen[-1]) != c_chars.index(c):
|
|
wrong_chars.append(c)
|
|
result += missing_val[c_chars.index(c)]
|
|
break
|
|
else:
|
|
del seen[-1]
|
|
print("Part 1 result:", result)
|
|
|
|
|
|
def part_2(input):
|
|
result = 0
|
|
scores = []
|
|
for line in input.copy():
|
|
seen = []
|
|
for c in line.rstrip():
|
|
if c in o_chars:
|
|
seen.append(c)
|
|
elif o_chars.index(seen[-1]) != c_chars.index(c):
|
|
input.remove(line)
|
|
break
|
|
else:
|
|
del seen[-1]
|
|
for line in input:
|
|
seen = []
|
|
for c in line.rstrip():
|
|
if c in o_chars:
|
|
seen.append(c)
|
|
elif o_chars.index(seen[-1]) == c_chars.index(c):
|
|
del seen[-1]
|
|
score = 0
|
|
for c in reversed(seen):
|
|
score *= 5
|
|
score += corrupted_val[o_chars.index(c)]
|
|
scores.append(score)
|
|
scores.sort()
|
|
result = scores[(len(scores) // 2)]
|
|
print("Part 2 result:", result)
|
|
|
|
|
|
input = list()
|
|
p = Path(__file__).with_name('input.txt')
|
|
with open(p) as f:
|
|
input = f.readlines()
|
|
part_1(input)
|
|
part_2(input)
|