From f82eaa9490c5a9f3ecbe7ac86342042d5df39cba Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 10 Dec 2021 06:32:54 +0100 Subject: [PATCH] 2021 day10/python: add solution --- 2021/day10/day10.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 2021/day10/day10.py diff --git a/2021/day10/day10.py b/2021/day10/day10.py new file mode 100755 index 0000000..674ac89 --- /dev/null +++ b/2021/day10/day10.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import sys + +if __name__ == '__main__': + syntax_check_score = 0 + autocomplete_scores = [] + for line in sys.stdin.readlines(): + stack = [] + corrupted = False + + for c in line.strip(): + if c in '([{<': + stack.append({'(': ')', '[': ']', '{': '}', '<': '>'}[c]) + elif stack.pop() != c: + syntax_check_score += {')': 3, ']': 57, '}': 1197, '>': 25137}[c] + corrupted = True + break + + if corrupted: continue + + autocomplete_score = 0 + for c in stack[::-1]: + autocomplete_score *= 5 + autocomplete_score += {')': 1, ']': 2, '}': 3, '>': 4}[c] + autocomplete_scores.append(autocomplete_score) + + print(syntax_check_score) + autocomplete_scores.sort() + print(autocomplete_scores[len(autocomplete_scores)//2])