10.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from util import get_input
  2. input = get_input("10.input")
  3. corresp = {
  4. ')': '(',
  5. ']': '[',
  6. '}': '{',
  7. '>': '<',
  8. }
  9. def find_illegal(line):
  10. stack = []
  11. for c in line:
  12. if c in ['(', '[', '{', '<']:
  13. stack.append(c)
  14. else:
  15. if len(stack) == 0:
  16. return None
  17. if stack[-1] != corresp[c]:
  18. return c
  19. else:
  20. stack.pop()
  21. return stack
  22. points = {
  23. ')': 3,
  24. ']': 57,
  25. '}': 1197,
  26. '>': 25137,
  27. }
  28. results = [find_illegal(line) for line in input]
  29. print("Part 1:", sum([points.get(c, 0) for c in results if type(c) == str]))
  30. remaining = [stack for stack in results if type(stack) == list]
  31. points = {
  32. '(': 1,
  33. '[': 2,
  34. '{': 3,
  35. '<': 4,
  36. }
  37. def get_score(stack):
  38. sum = 0
  39. while len(stack) > 0:
  40. sum *= 5
  41. sum += points[stack.pop()]
  42. return sum
  43. scores = [get_score(s) for s in remaining]
  44. scores.sort()
  45. print("Part 2:", scores[int(len(scores) / 2)])