Advent of Code 2021 Python Solution: Day 10

qviper

Viper

Posted on December 10, 2021

Advent of Code 2021 Python Solution: Day 10

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

Why not read more?

💖 💪 🙅 🚩
qviper
Viper

Posted on December 10, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of Code 2021 Python Solution: Day 16
adventofcode Advent of Code 2021 Python Solution: Day 16

December 17, 2021

Advent of Code 2021 Python Solution: Day 15
adventofcode Advent of Code 2021 Python Solution: Day 15

December 15, 2021

Advent of Code 2021 Python Solution: Day 14
adventofcode Advent of Code 2021 Python Solution: Day 14

December 14, 2021

Advent of Code 2021 Python Solution: Day 13
adventofcode Advent of Code 2021 Python Solution: Day 13

December 13, 2021

Advent of Code 2021 Python Solution: Day 12
adventofcode Advent of Code 2021 Python Solution: Day 12

December 12, 2021