-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
34 lines (26 loc) · 1.19 KB
/
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
def isContained(firstPair, secondPair):
if firstPair[0] <= secondPair[0] <= firstPair[1] and firstPair[0] <= secondPair[1] <= firstPair[1]:
return True
if secondPair[0] <= firstPair[0] <= secondPair[1] and secondPair[0] <= firstPair[1] <= secondPair[1]:
return True
return False
def isOverlap(firstPair, secondPair):
if secondPair[1] >= firstPair[1] >= secondPair[0] or firstPair[1] >= secondPair[1] >= firstPair[0]:
return True
if secondPair[1] >= firstPair[0] >= secondPair[0] or firstPair[1] >= secondPair[0] >= firstPair[0]:
return True
return isContained(firstPair, secondPair)
if __name__ == '__main__':
f = open('./day4_input.txt', 'r')
lines = f.readlines()
fullyContainedPairs = 0
overlappingPairs = 0
for l in lines:
firstPair = l.strip().split(',')[0].split('-')
secondPair = l.strip().split(',')[1].split('-')
if isContained([eval(i) for i in firstPair], [eval(i) for i in secondPair]):
fullyContainedPairs += 1
if isOverlap([eval(i) for i in firstPair], [eval(i) for i in secondPair]):
overlappingPairs += 1
print(fullyContainedPairs)
print(overlappingPairs)