summaryrefslogtreecommitdiff
path: root/2022/03/rucksack.py
blob: 30efa9281f6a06dfbf7105d53b4b24e861ab1f45 (plain)
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
def priority(c):
    if (ord(c) >= ord("a")):
        return ord(c) - ord("a") + 1
    else:
        return ord(c) - ord("A") + 27
def same(x, y):
    a = set()
    for x1 in x:
        if x1 in y:
            a.add(x1)
    return list(a)
def inlists(x, y):
    isin = []
    for x1 in x:
        if all([x1 in y1 for y1 in y]):
            isin.append(x1)
    if len(isin) > 0:
        return isin[0]
    return None

with open("input") as f:
    compartments = [line.strip() for line in f.readlines()]
points = 0
for i in range(0, len(compartments), 3):
    group = compartments[i:i+3]
    if len(group) == 3:
        c = inlists(group[0], group[1:])
        if c is not None:
            points += priority(c)
print(points)