Skip to content

Commit 9bbdf9c

Browse files
committed
Adding solution for Day 4 Part 2
1 parent de8200d commit 9bbdf9c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Day4/Part2.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from pathlib import Path
2+
3+
from Day4 import Part1
4+
5+
def solve(file_path:Path) -> int:
6+
''' Finds the number of overlapping sections for elf cleaning assignments.
7+
8+
Arguments:
9+
file_path(pathlib.Path): The filepath in which to read data from.
10+
11+
Returns:
12+
Integer representing the answer.
13+
'''
14+
cleaning_assignments = Part1.read_data(file_path)
15+
16+
return (sum([cleaning_overlaps(assignment) for assignment in cleaning_assignments]))
17+
18+
def cleaning_overlaps(cleaning_assignment) -> bool:
19+
''' Determines if one cleaning assignment overlaps
20+
21+
Arguments:
22+
cleaning_assignment(tuple(tuple(int))): A tuple of tuples containing integers for each cleaning assignment.
23+
24+
Returns:
25+
Returns a boolean if one cleaning assignment overlaps the other
26+
'''
27+
first_elf = cleaning_assignment[0]
28+
second_elf = cleaning_assignment[1]
29+
30+
if first_elf[0] <= second_elf[0] <= first_elf[1]:
31+
return True
32+
if first_elf[0] <= second_elf[1] <= first_elf[1]:
33+
return True
34+
if second_elf[0] <= first_elf[0] <= second_elf[1]:
35+
return True
36+
if second_elf[0] <= first_elf[1] <= second_elf[1]:
37+
return True
38+
39+
return False

run.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44

55

6-
from Day3 import Part1
6+
from Day4 import Part2
77

8-
print(Part1.solve(Path() / 'Day3' / 'input.txt'))
8+
print(Part2.solve(Path() / 'Day4' / 'input.txt'))

0 commit comments

Comments
 (0)