-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
31 lines (26 loc) · 1.31 KB
/
main.py
File metadata and controls
31 lines (26 loc) · 1.31 KB
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
import time
def main(input_file, stage):
with open(input_file, "r") as file:
# if we are asking which groups are overlapping the second item for elf1 needs to be smaller
# or equal to the second item for elf 2. if we are asking for contained only the first item of
# elf1 needs to be smaller or equal to the second item for elf 2 (basically: if the range is partially
# included the first item needs to in range of the second elf's range)
contained_or_overlap = 1 if stage == 1 else 0
cnt = 0
for assignment in file.readlines():
elf1, elf2 = assignment.strip().split(",")
elf1, elf2 = (elf1.split("-"), elf2.split("-"))
elf1, elf2 = (int(elf1[0]), int(elf1[1])), (int(elf2[0]), int(elf2[1]))
if (elf1[0] >= elf2[0] and elf1[contained_or_overlap] <= elf2[1]) or \
(elf2[0] >= elf1[0] and elf2[contained_or_overlap] <= elf1[1]):
cnt += 1
print(cnt)
if __name__ == "__main__":
use_example = False
file_name = "example" if use_example else "input"
start = time.time()
main(file_name, 1) # result: 605
print(f"Stage 1 time: {time.time() - start}")
start = time.time()
main(file_name, 2) # result: 914
print(f"Stage 2 time: {time.time() - start}")