Skip to content

Commit 8f80d1e

Browse files
committed
day04 - using sets
makes the code more readable
1 parent 4af9c66 commit 8f80d1e

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

day04/part1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def compute(s: str) -> int:
2525
overlaps = 0
2626
for line in lines:
2727
a, b, x, y = map(int, re.findall(r'\d+', line))
28-
if (x <= a <= y and x <= b <= y) or (a <= x <= b and a <= y <= b):
28+
A, B = set(range(a, b + 1)), set(range(x, y + 1))
29+
if A.issubset(B) or B.issubset(A):
2930
overlaps += 1
3031
return overlaps
3132

day04/part2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def compute(s: str) -> int:
2525
overlaps = 0
2626
for line in lines:
2727
a, b, x, y = map(int, re.findall(r'\d+', line))
28-
if (x <= a <= y or x <= b <= y) or (a <= x <= b or a <= y <= b):
28+
A, B = set(range(a, b + 1)), set(range(x, y + 1))
29+
if A.intersection(B):
2930
overlaps += 1
3031
return overlaps
3132

0 commit comments

Comments
 (0)