My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2021/10

+51
+51
2021/day10.py
··· 1 + import fileinput 2 + 3 + RIGHT_TO_LEFT = { 4 + '}': '{', 5 + ']': '[', 6 + ')': '(', 7 + '>': '<', 8 + } 9 + 10 + LEFT_TO_RIGHT = {v: k for k, v in RIGHT_TO_LEFT.items()} 11 + 12 + ILLEGAL_POINTS = { 13 + ')': 3, 14 + ']': 57, 15 + '}': 1197, 16 + '>': 25137, 17 + } 18 + 19 + PART_2_POINTS = { 20 + ')': 1, 21 + ']': 2, 22 + '}': 3, 23 + '>': 4, 24 + } 25 + 26 + part_1 = 0 27 + part_2_scores = [] 28 + 29 + for line in fileinput.input(): 30 + stk = [] 31 + is_incomplete = True 32 + 33 + for c in line.strip(): 34 + if c in RIGHT_TO_LEFT.values(): 35 + stk.append(c) 36 + else: 37 + p = stk.pop() 38 + if RIGHT_TO_LEFT[c] != p: 39 + is_incomplete = False 40 + part_1 += ILLEGAL_POINTS[c] 41 + 42 + if is_incomplete: 43 + score = 0 44 + for s in stk[::-1]: 45 + score *= 5 46 + score += PART_2_POINTS[LEFT_TO_RIGHT[s]] 47 + 48 + part_2_scores.append(score) 49 + 50 + print "Part 1:", part_1 51 + print "Part 2:", sorted(part_2_scores)[len(part_2_scores)//2]