5

Advent of Code 2021 Python Solution: Day 10

 2 years ago
source link: https://dev.to/qviper/advent-of-code-2021-python-solution-day-10-5h6d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

I forgot the time of the challenge but still managed to make it done. Stack came to the aid.

Part 1

data,data1=get_data(day=10)


table = {
    ")": 3,
    "]": 57,
    "}": 1197,
    ">": 25137}

pair = {"(":")","{":"}", "[":"]", "<":">"}


corruptions = []
rem = []
for i,r in enumerate(data):
    stack = []
    is_corr=False
    for c in r:
        if c in pair:
            stack.append(pair[c])
        elif stack.pop() != c:
            print(f"Corrupted {c} at row {i}")
            corruptions.append(c)
            is_corr=True
            break
    if is_corr==False and len(stack)>0:
        rem.append(stack)


corr = dict(Counter(corruptions))
sum([table[k]*v for k,v in corr.items()])

Enter fullscreen mode

Exit fullscreen mode

Part 2

mult = {")": 1,
"]": 2,
"}": 3,
">": 4}
all_total=[]
for row in rem:
    s = 0
    for i,c in enumerate(row):
        s+=5**i*mult[c]
    all_total.append(s)
at = sorted(all_total)
at[len(at)//2]

Enter fullscreen mode

Exit fullscreen mode


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK