Advertisement
SansPapyrus683

Advent of Code 2021 Day 10 Python Solution

Dec 9th, 2021
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. from statistics import median
  2.  
  3. OPENING = "([{<"
  4. CLOSING = ")]}>"
  5. P1_SCORES = {c: s for c, s in zip(CLOSING, [3, 57, 1197, 25137])}
  6. P2_SCORES = {c: s for c, s in zip(OPENING, [1, 2, 3, 4])}
  7. CORR_OPENING = {c: o for c, o in zip(CLOSING, OPENING)}
  8.  
  9. with open("building_snowmen.txt") as read:
  10.     nav_subs = [r.strip() for r in read.readlines()]
  11.  
  12. total_error = 0
  13. closing_scores = []
  14. for s in nav_subs:
  15.     curr_openings = []
  16.     for c in s:
  17.         if c in OPENING:
  18.             curr_openings.append(c)
  19.         elif c in CLOSING:
  20.             if not curr_openings or CORR_OPENING[c] != curr_openings[-1]:
  21.                 total_error += P1_SCORES[c]
  22.                 break
  23.             else:
  24.                 curr_openings.pop()
  25.     else:
  26.         score = 0
  27.         for c in reversed(curr_openings):
  28.             score = (score * 5) + P2_SCORES[c]
  29.         closing_scores.append(score)
  30.  
  31. print(total_error)
  32. print(median(closing_scores))
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement