This commit is contained in:
64
day-10/day-10.py
Normal file
64
day-10/day-10.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/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
|
||||
lines = input.copy()
|
||||
scores = []
|
||||
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):
|
||||
lines.remove(line)
|
||||
break
|
||||
else:
|
||||
del seen[-1]
|
||||
for line in lines:
|
||||
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)
|
||||
Reference in New Issue
Block a user