-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.cpp
More file actions
67 lines (52 loc) · 1.8 KB
/
Copy pathday4.cpp
File metadata and controls
67 lines (52 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Example input
/*
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
*/
#include <iostream>
#include <fstream>
#include <string>
const std::string inputFilePath = "inputs/day4.txt";
bool isPairContainedWithinOtherPair(std::pair<int, int> p1, std::pair<int, int> p2){
return ((p1.first >= p2.first) && (p1.second <= p2.second));
}
bool isThereOverlap(std::pair<int, int> p1, std::pair<int, int> p2){
for(int i=p1.first; i <= p1.second; i++){
if((i >= p2.first) && (i <= p2.second)){
return true;
}
}
return false;
}
int main(){
int containedAssignments = 0;
int overlapAssignments = 0;
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string line;
while(std::getline(ifs, line)){
// Parse input
std::pair<int, int> elf1Assignment;
std::pair<int, int> elf2Assignment;
int elf1LoweBound = std::stoi(line.substr(0, line.find('-')));
int elf1UpperBound = std::stoi(line.substr(line.find('-') + 1, line.find(',')));
int elf2LoweBound = std::stoi(line.substr(line.find(',') + 1, line.rfind('-')));
int elf2UpperBound = std::stoi(line.substr(line.rfind('-') + 1, line.length()));
elf1Assignment = {elf1LoweBound, elf1UpperBound};
elf2Assignment = {elf2LoweBound, elf2UpperBound};
// Ex1
if ((isPairContainedWithinOtherPair(elf1Assignment, elf2Assignment)) || (isPairContainedWithinOtherPair(elf2Assignment, elf1Assignment))){
containedAssignments++;
}
// Ex2
if (isThereOverlap(elf1Assignment, elf2Assignment)){
overlapAssignments++;
}
}
std::cout << "--Ex1 Output: " << containedAssignments << std::endl;
std::cout << "--Ex2 Output: " << overlapAssignments << std::endl;
return 0;
}