""" count all supersets in given range pairs """ import re,os pattern = re.compile(r'(\d+)-(\d+),(\d+)-(\d+)') # return one tuple with all four numbers # pattern = re.compile(r'(\d+)-(\d+)') # return two tuples with two numbers each with open('./2022/4/assigned_sections.txt') as filein: text = filein.read() # text = """2-4,6-8 # 2-3,4-5 # 5-7,7-9 # 2-8,3-7 # 6-6,4-6 # 2-6,4-8""" lines = text.splitlines() assigments = [tuple(int(e) for e in line) for line in pattern.findall(text)] c = 0 for a0,a1,b0,b1 in assigments: r1, r2 = set(range(a0,a1+1)), set(range(b0,b1+1)) if r1.issubset(r2) or r1.issuperset(r2): c+=1 print(c) """ now count all overlapping sets """ c=0 for a0,a1,b0,b1 in assigments: r1,r2 = set(range(a0,a1+1)), set(range(b0,b1+1)) if r1.intersection(r2): c+=1 print(c)