2022: Day 1 - 4

This commit is contained in:
ngb
2022-12-04 07:27:32 +01:00
parent bb8f6e1e8a
commit 78279014e0
12 changed files with 6271 additions and 0 deletions

30
2022/day4_2.py Normal file
View File

@@ -0,0 +1,30 @@
## Read input file
input = []
with open('day4_input.txt', 'r') as f:
for line in f.readlines():
input.append(line)
# Solve problem
def run():
count = 0
for line in input:
first, second = line.split(',')
first = make_range(first)
second = make_range(second)
if overlaps(first, second) or overlaps(second, first):
count += 1
print(count)
def make_range( a ):
return tuple(int(i) for i in a.split('-'))
def overlaps( r1, r2 ):
return not (r2[1] < r1[0] or r2[0] > r1[1])
if __name__ == '__main__':
run()