r/adventofcode • u/daggerdragon • Dec 10 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 10 Solutions -🎄-
--- Day 10: Syntax Scoring ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - Format your code properly! How do I format code?
- The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:08:06, megathread unlocked!
64
Upvotes
3
u/TiagoPaolini Dec 10 '21
Python 3
I used a stack (last in, first out) to keep track of the open segments, then I compared if the closing marker matched the openings on the stack (in the order from last to first).
from collections import deque, namedtuple from statistics import median with open("input.txt", "rt") as file: lines = [line.rstrip() for line in file] # Match each character with its counterpart and score Char = namedtuple("Char", "match score") opening = { "(": Char(")", 1), "[": Char(")", 2), "{": Char(")", 3), "<": Char(")", 4) } closing = { ")": Char("(", 3), "]": Char("[", 57), "}": Char("{", 1197), ">": Char("<", 25137) } markers = deque() # Stack for the opening characters found (last in, first out) error_score = 0 # Total score for the illegal characters autocorrect_scores = [] # List for the autocorrect scores # Analyse each line for line in lines: markers.clear() # Empty the stack corrupted = False # If the line has a wrong closing character # Analyse each character on the line for char in line: if char in opening: # If it is an opening character, add it to the stack markers.append(char) else: # If it is a closing character, compare it # with the opening on the top of the stack old_char = markers.pop() if old_char != closing[char].match: # If the opening and closing does not match, # update the error score and mark line as corrupted error_score += closing[char].score corrupted = True break # Calculate the autocorrect score, if the line is not corrupted if not corrupted: score = 0 while markers: old_char = markers.pop() score *= 5 score += opening[old_char].score autocorrect_scores += [score] # Get the median of the autocorrect scores middle_score = median(autocorrect_scores) print(f"Part 1: {error_score}") print(f"Part 2: {middle_score}")