123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- from util import get_input
- input = get_input("10.input")
- corresp = {
- ')': '(',
- ']': '[',
- '}': '{',
- '>': '<',
- }
- def find_illegal(line):
- stack = []
- for c in line:
- if c in ['(', '[', '{', '<']:
- stack.append(c)
- else:
- if len(stack) == 0:
- return None
- if stack[-1] != corresp[c]:
- return c
- else:
- stack.pop()
- return stack
- points = {
- ')': 3,
- ']': 57,
- '}': 1197,
- '>': 25137,
- }
- results = [find_illegal(line) for line in input]
- print("Part 1:", sum([points.get(c, 0) for c in results if type(c) == str]))
- remaining = [stack for stack in results if type(stack) == list]
- points = {
- '(': 1,
- '[': 2,
- '{': 3,
- '<': 4,
- }
- def get_score(stack):
- sum = 0
- while len(stack) > 0:
- sum *= 5
- sum += points[stack.pop()]
- return sum
- scores = [get_score(s) for s in remaining]
- scores.sort()
- print("Part 2:", scores[int(len(scores) / 2)])
|