aboutsummaryrefslogtreecommitdiff
path: root/2021/10.py
blob: 019abbc53599cf859982f5ef35bcea279b3d82ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Day Ten: Syntax Scoring

file = open("input/10.txt").readlines()

score = 0
completions = []
for line in file:
    expected = ""
    for char in line:
        if char == "(":
            expected += ")"
        elif char == "[":
            expected += "]"
        elif char == "{":
            expected += "}"
        elif char == "<":
            expected += ">"
        elif char == expected[-1]:
            expected = expected[0:-1]
        else:
            if char == ")":
                score += 3
            elif char == "]":
                score += 57
            elif char == "}":
                score += 1197
            elif char == ">":
                score += 25137
            else:
                completions.append(expected[::-1])
            break
print(score)

scores = []
for seq in completions:
    score = 0
    for char in seq:
        score *= 5
        if char == ")":
            score += 1
        elif char == "]":
            score += 2
        elif char == "}":
            score += 3
        elif char == ">":
            score += 4
    scores.append(score)
print(sorted(scores)[len(scores)//2])