with open("day4\input.txt") as f: data = f.readlines() ''' It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all. In how many assignment pairs do the ranges overlap? ''' pair_sum = 0 line:str for line in data: elf1 = [int(i) for i in line.split(',')[0].split('-')] elf2 = [int(i) for i in line.split(',')[1].split('-')] #Example 77-88,14-78 # elf1 = 77-88 # elf2 = 14-78 #Find the smallest if elf1[1] - elf1[0] > elf2[1] - elf2[0]: if elf2[0] >= elf1[0] and elf2[1] <= elf1[1]: pair_sum += 1 continue else: if elf1[0] >= elf2[0] and elf1[1] <= elf2[1]: pair_sum += 1 continue #Look at elf 2's numbers and see if they are in elf 1's range if elf1[0] <= elf2[0] <= elf1[1] or elf1[0] <= elf2[1] <= elf1[1]: pair_sum +=1 print(pair_sum) #931