mirror of
https://github.com/jneug/adventofcode.git
synced 2026-04-14 06:43:33 +02:00
2022: Day 1 - 4
This commit is contained in:
30
2022/day4_2.py
Normal file
30
2022/day4_2.py
Normal 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()
|
||||
Reference in New Issue
Block a user