1
+ from pathlib import Path
2
+ from typing import List
3
+
4
+ def solve (file_path :Path ) -> int :
5
+ ''' Finds the number of overlapping sections for elf cleaning assignments.
6
+
7
+ Arguments:
8
+ file_path(pathlib.Path): The filepath in which to read data from.
9
+
10
+ Returns:
11
+ Integer representing the answer.
12
+ '''
13
+ cleaning_assignments = read_data (file_path )
14
+
15
+ return (sum ([cleaning_contains (assignment ) for assignment in cleaning_assignments ]))
16
+
17
+
18
+ def read_data (data_file_path :Path ) -> List :
19
+ ''' Reads in the data for the specific problem and returns the most valuable data type
20
+
21
+ Day 4 has us reading in two elves range of cleaning zones. I think the best way to order them is a list of tuples of tuples containing integers.
22
+
23
+ Arguments:
24
+ data_file_path(pathlib.Path): A path to the datafile to read in.
25
+
26
+ Returns:
27
+ Returns a list representing the data file in the most useful way possible.
28
+ '''
29
+ output = []
30
+ with open (data_file_path , 'r' , encoding = 'UTF-8' ) as data_file :
31
+ for line in data_file .readlines ():
32
+ elves = line .split (',' )
33
+ elves = [tuple ([int (section ) for section in elf .split ('-' )]) for elf in elves ]
34
+ output .append (tuple (elves ))
35
+
36
+ return output
37
+
38
+
39
+ def cleaning_contains (cleaning_assignment ) -> bool :
40
+ ''' Determines if one cleaning assignment contains the other.
41
+
42
+ Arguments:
43
+ cleaning_assignment(tuple(tuple(int))): A tuple of tuples containing integers for each cleaning assignment.
44
+
45
+ Returns:
46
+ Returns a boolean if one cleaning assignment fully contains the other.
47
+ '''
48
+ first_elf = cleaning_assignment [0 ]
49
+ second_elf = cleaning_assignment [1 ]
50
+
51
+ # If our first elf contains our second elf
52
+ if first_elf [0 ] <= second_elf [0 ]:
53
+ if first_elf [1 ] >= second_elf [1 ]:
54
+ return True
55
+
56
+ # If our second elf contains our first elf
57
+ if second_elf [0 ] <= first_elf [0 ]:
58
+ if second_elf [1 ] >= first_elf [1 ]:
59
+ return True
60
+
61
+ # If we haven't returned at this point, we don't have an assignment that contains another, we can return false.
62
+ return False
0 commit comments