Day4

简介

第一部分

大意就是找出下面配对序列中 范围有完全包含另一个序列的个数

  • 2-4,6-8
  • 2-3,4-5
  • 5-7,7-9
  • 2-8,3-7
  • 6-6,4-6
  • 2-6,4-8

上面给出的配对中 只有2-8, 3-76-6,4-6 是完全包含的 因此存在 2 种

第二部分

寻找有重叠部分(overlap at all)的配对

  • 5-7,7-9 :重叠部分为 7.
  • 2-8,3-7 :重叠部分为 3 到 7.
  • 6-6,4-6 :重叠部分为 6.
  • 2-6,4-8 :重叠部分为 是 4, 5, and 6

因此包含此种配对一共有四种

解法

Part I

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def solution_part_1(data):
    count = 0
    for i in data.split("\n"):
        first, second = i.split(",")
        first_list = list(
            map(str, range(int(first.split("-")[0]), int(first.split("-")[1]) + 1))
        )
        second_list = list(
            map(str, range(int(second.split("-")[0]), int(second.split("-")[1]) + 1))
        )
        interset_set = set(first_list).intersection(set(second_list))
        if interset_set == set(first_list) or interset_set == set(second_list):
            count += 1
    return count

Part II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

def solution_part_2(data):
    count = 0
    for i in data.split("\n"):
        first, second = i.split(",")
        first_list = list(
            map(str, range(int(first.split("-")[0]), int(first.split("-")[1]) + 1))
        )
        second_list = list(
            map(str, range(int(second.split("-")[0]), int(second.split("-")[1]) + 1))
        )
        interset_set = set(first_list).intersection(set(second_list))
        if len(interset_set) > 0:
            count += 1
    return count

总结

  • 问题可以转换数学集合中的交集和子集判断