summaryrefslogtreecommitdiffstats
path: root/3.py
blob: e7bb2d1bc8c584f53d8ccafe96ec984f8f7bbdca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
with open('3.in') as f:
    rucksacks = [line.rstrip() for line in f]

def priority(itm):
    if itm.islower():
        return ord(common) - ord('a') + 1
    else:
        return ord(common) - ord('A') + 27

total = 0
for rucksack in rucksacks:
    half = len(rucksack) // 2
    a, b = set(rucksack[:half]), set(rucksack[half:])
    common = set.intersection(a, b).pop()
    total += priority(common)
print(total)

total = 0
for i in range(0, len(rucksacks), 3):
    a, b, c = map(set, rucksacks[i:i+3])
    common = set.intersection(a, set.intersection(b, c)).pop()
    total += priority(common)
print(total)