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