rucksack.py (727B)
1 def priority(c): 2 if (ord(c) >= ord("a")): 3 return ord(c) - ord("a") + 1 4 else: 5 return ord(c) - ord("A") + 27 6 def same(x, y): 7 a = set() 8 for x1 in x: 9 if x1 in y: 10 a.add(x1) 11 return list(a) 12 def inlists(x, y): 13 isin = [] 14 for x1 in x: 15 if all([x1 in y1 for y1 in y]): 16 isin.append(x1) 17 if len(isin) > 0: 18 return isin[0] 19 return None 20 21 with open("input") as f: 22 compartments = [line.strip() for line in f.readlines()] 23 points = 0 24 for i in range(0, len(compartments), 3): 25 group = compartments[i:i+3] 26 if len(group) == 3: 27 c = inlists(group[0], group[1:]) 28 if c is not None: 29 points += priority(c) 30 print(points)