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)