-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.py
52 lines (41 loc) · 1.63 KB
/
day04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import utils
assignments = utils.load_input("day04")
def overlap_completely(assignnment: tuple[tuple[int, int], tuple[int, int]]) -> bool:
first_elf, second_elf = assignnment
first_elf_left, first_elf_right = first_elf
second_elf_left, second_elf_right = second_elf
return (
first_elf_left <= second_elf_left <= second_elf_right <= first_elf_right
or second_elf_left <= first_elf_left <= first_elf_right <= second_elf_right
)
def overlap_partially(assignnment: tuple[tuple[int, int], tuple[int, int]]) -> bool:
first_elf, second_elf = assignnment
first_elf_left, first_elf_right = first_elf
second_elf_left, second_elf_right = second_elf
return (
first_elf_left <= second_elf_left <= first_elf_right
or first_elf_left <= second_elf_right <= first_elf_right
or second_elf_left <= first_elf_left <= second_elf_right
or second_elf_left <= first_elf_right <= second_elf_right
)
def part1(text: str) -> int:
assignments = [
tuple(
(int(elf.split("-")[0]), int(elf.split("-")[-1]))
for elf in assignment.split(",")
)
for assignment in text.split("\n")
]
return sum(overlap_completely(assignment) for assignment in assignments)
def part2(text: str) -> int:
assignments = [
tuple(
(int(elf.split("-")[0]), int(elf.split("-")[-1]))
for elf in assignment.split(",")
)
for assignment in text.split("\n")
]
return sum(overlap_partially(assignment) for assignment in assignments)
if __name__ == "__main__":
print(part1(assignments))
print(part2(assignments))