From f9e4204dd9b107174ad2d6f31c7a5068b2fe40da Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Thu, 9 Dec 2021 23:13:41 -0800 Subject: solution for day 10 --- day10.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 day10.py diff --git a/day10.py b/day10.py new file mode 100644 index 0000000..5cb1073 --- /dev/null +++ b/day10.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +from collections import deque + +pairs = { + "(" : ")", + "[" : "]", + "{" : "}", + "<" : ">" +} + +scores = { + ")": 3, + "]": 57, + "}": 1197, + ">": 25137 +} + +scores2 = { + ")": 1, + "]": 2, + "}": 3, + ">": 4 +} + +# part 1 +score = 0 +completion_scores = [] +with open("day10.txt") as data: + for line in data: + stack = deque() + invalid = False + for char in line.strip(): + if char in pairs.keys(): + stack.append(char) + if char in pairs.values(): + if len(stack) == 0 or pairs[stack[-1]] != char: + score += scores[char] + invalid = True + break + elif pairs[stack[-1]] == char: + stack.pop() + # part 2 + if not invalid: + total = 0 + while stack: + total = (5 * total) + scores2[pairs[stack.pop()]] + completion_scores.append(total) + +print(score) +completion_scores.sort() +print(completion_scores[(len(completion_scores) - 1) // 2]) -- cgit 1.4.1-2-gfad0