diff options
Diffstat (limited to '3.py')
-rw-r--r-- | 3.py | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -0,0 +1,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) |