from string import ascii_letters sum_of_priorities = 0 def get_3_rucksacks(fh): return [next(fh).strip() for _ in range(3)] with open("3.in") as fh: while True: try: rucksacks = get_3_rucksacks(fh) except StopIteration: break a, b, c = rucksacks item_in_all = list(set(a).intersection(set(b)).intersection(set(c)))[0] priority = ascii_letters.find(item_in_all) + 1 sum_of_priorities += priority print(sum_of_priorities)