Add solutions for four day tasks
This commit is contained in:
parent
2404bf2223
commit
6fa8be5c65
1000
04/input-prod
Normal file
1000
04/input-prod
Normal file
File diff suppressed because it is too large
Load Diff
6
04/input-test
Normal file
6
04/input-test
Normal file
@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
58
04/task.py
Normal file
58
04/task.py
Normal file
@ -0,0 +1,58 @@
|
||||
def load(name):
|
||||
with open(name, 'r') as f:
|
||||
for line in f.readlines():
|
||||
if line:
|
||||
f, s = line.split(',')
|
||||
x1, y1 = f.split('-')
|
||||
x2, y2 = s.split('-')
|
||||
yield (int(x1), int(y1), int(x2), int(y2))
|
||||
|
||||
|
||||
def check_full_overlap(x1, y1, x2, y2):
|
||||
return (
|
||||
(x1 >= x2 and y1 <= y2) or (x2 >= x1 and y2 <= y1)
|
||||
)
|
||||
|
||||
|
||||
def solution_1(name):
|
||||
return sum([
|
||||
int(check_full_overlap(*g)) for g in load(name)
|
||||
])
|
||||
|
||||
# ....=====..
|
||||
# ======.....
|
||||
|
||||
# ====.......
|
||||
# ..======
|
||||
|
||||
def check_partial_overlap(x1, y1, x2, y2):
|
||||
return (x1 <= y2 and y1 >= x2) or (y1 >= x2 and x1 <= y2)
|
||||
|
||||
|
||||
|
||||
def solution_2(name):
|
||||
return sum([
|
||||
int(check_partial_overlap(*g)) for g in load(name)
|
||||
])
|
||||
|
||||
|
||||
# ASSERT
|
||||
|
||||
assert check_partial_overlap(5, 7, 7, 9) == True
|
||||
assert check_partial_overlap(2, 8, 3, 7) == True
|
||||
assert check_partial_overlap(6, 6, 4, 6) == True
|
||||
assert check_partial_overlap(2, 6, 4, 8) == True
|
||||
|
||||
assert check_partial_overlap(2, 4, 6, 8) == False
|
||||
assert check_partial_overlap(2, 3, 4, 5) == False
|
||||
|
||||
print(solution_2('input-test'))
|
||||
|
||||
assert solution_1('input-test') == 2
|
||||
assert solution_2('input-test') == 4
|
||||
|
||||
|
||||
# RUN
|
||||
|
||||
print(solution_1('input-prod'))
|
||||
print(solution_2('input-prod'))
|
Loading…
Reference in New Issue
Block a user