-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2022_day4.py
54 lines (42 loc) · 1.49 KB
/
2022_day4.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
53
54
def contained(sections):
contained = 0
for i in range(len(sections)):
S=sections[i].split(",")
s0= S[0].split("-")
s1= S[1].split("-")
#convert to int. If this step is not done, the result will be incorrect because it will compare strings
s0[0] = int(s0[0])
s0[1] = int(s0[1])
s1[0] = int(s1[0])
s1[1] = int(s1[1])
if ((s0[0] <= s1[0] <= s0[1]) and (s0[0] <= s1[1] <= s0[1])) or ((s1[0] <= s0[0] <= s1[1]) and (s1[0] <= s0[1] <= s1[1])):
contained = contained + 1
return contained
def overlap(sections):
overlapped = 0
for i in range(len(sections)):
S=sections[i].split(",")
s0= S[0].split("-")
s1= S[1].split("-")
#convert to int. If this step is not done, the result will be incorrect because it will compare strings
s0[0] = int(s0[0])
s0[1] = int(s0[1])
s1[0] = int(s1[0])
s1[1] = int(s1[1])
#check if there's an overlap
if (s0[0] <= s1[1]) and (s0[1] >= s1[0]):
overlapped = overlapped + 1
return overlapped
def __main__():
input_file = open("day4input.txt", "r")
content = input_file.read()
sections = content.split("\n")
print(contained(sections))
print(overlap(sections))
input_file = open("day4_test.txt", "r")
content = input_file.read()
sections = content.split("\n")
print(contained(sections))
print(overlap(sections))
if __name__ == "__main__":
__main__()