]> git.friedersdorff.com Git - max/aoc_2022.git/blob - 4_2.py
Do day 7
[max/aoc_2022.git] / 4_2.py
1 """
2 Calculate the number of assignment pairs, where
3 one range fully contains the other.
4 """
5
6 overlaps = 0
7
8 def compare(elf0, elf1):
9     return elf1["start"] <= elf0["start"] <= elf1["end"]
10
11
12 with open("4.in") as fh:
13     for line in fh:
14         line = line.strip()
15
16         elves = []
17         for elf in line.split(","):
18             elf_s, elf_e = [int(x) for x in elf.split("-")]
19             elves.append({
20                 "start": elf_s,
21                 "end": elf_e
22             })
23
24         elf0_overlaps = compare(elves[0], elves[1])
25         elf1_overlaps = compare(elves[1], elves[0])
26
27         if elf0_overlaps or elf1_overlaps:
28             overlaps += 1
29
30 print(overlaps)