Advertisement
illuminati229

Untitled

Dec 3rd, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. def section_compare(filepath, any_overlap=False):
  2.     with open(filepath) as fin:
  3.         lines = fin.readlines()
  4.         elves = [line.strip().split(',') for line in lines]
  5.         elf_sets = [[set(range(int(a.split('-')[0]), int(a.split('-')[1]) + 1)), set(range(int(b.split('-')[0]), int(b.split('-')[1]) + 1))] for a, b in elves]
  6.  
  7.         contain_count = 0
  8.         if not any_overlap:
  9.             for ea, eb in elf_sets:
  10.                 len_a = len(ea)
  11.                 len_b = len(eb)
  12.                 len_c = len(ea & eb)
  13.                 if len_c == len_a or len_c == len_b:
  14.                     contain_count += 1
  15.         else:
  16.             for ea, eb in elf_sets:
  17.                 if len(ea & eb):
  18.                     contain_count += 1
  19.  
  20.     return contain_count
  21.  
  22.  
  23. def main():
  24.     assert section_compare('test.txt') == 2
  25.     print(section_compare('input.txt'))
  26.  
  27.     assert section_compare('test.txt', True) == 4
  28.     print(section_compare('input.txt', True))
  29.  
  30.  
  31. if __name__ == '__main__':
  32.     main()
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement