-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4b.py
28 lines (23 loc) · 869 Bytes
/
day4b.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
import re
def has_overlap(t1, t2):
if (((t1[0] >= t2[0] and t1[0] <= t2[1])) or ((t1[1] >= t2[0] and t1[1] <= t2[1])) or \
((t2[0] >= t1[0] and t2[0] <= t1[1])) or ((t2[1] >= t1[0] and t2[1] <= t1[1]))):
olap = True
print(f"FOUND!", t1, t2)
else:
olap = False
return(olap)
tally = 0
with open('day4a_input.txt', 'r') as fp:
for line in fp:
match = re.search(r"^(.*)-(.*),(.*)-(.*)", line.strip())
if match:
#print(f"found! {match.group(0)} {match.group(1)} {match.group(2)}")
elf1 = (int(match.group(1)), int(match.group(2)))
elf2 = (int(match.group(3)), int(match.group(4)))
#print(elf1, elf2)
if (has_overlap(elf1, elf2)):
tally += 1
else:
print(f"Bad input: {line}")
print(f"Total overlaps: {tally}")