Solution for day 04

This commit is contained in:
Maya Herrscher committed 2022-12-04 08:35:35 +01:00
1 parent 230daf810d
commit cab8e7ad77
3 files changed
+1100

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
import sys
def f_overlap(pair):
e1, e2 = pair
l1, h1 = e1
l2, h2 = e2
if l1 <= l2 and h1 >= h2: return True
if l2 <= l1 and h2 >= h1: return True
return False
def overlap(pair):
e1, e2 = pair
l1, h1 = e1
l2, h2 = e2
if l1 <= h2 and h1 >= l2: return True
if l2 <= h1 and h2 >= l1: return True
return False
if __name__ == '__main__':
lines = [line.strip('\n') for line in open(sys.argv[1])]
pairs = [tuple([tuple([int(n) for n in e.split('-')]) for e in l.split(',')]) for l in lines]
# challenge 1
fopairs = list(map(f_overlap,pairs))
res1 = str(sum(fopairs))
print("challenge 1:" + "\n" + res1 + "\n")
# challenge 2
opairs = list(map(overlap,pairs))
res2 = str(sum(opairs))
print("challenge 2:" + "\n" + res2 + "\n")