Day 5 Question 2 (late)
This commit is contained in:
1187
Day5/Question2/input
Normal file
1187
Day5/Question2/input
Normal file
File diff suppressed because it is too large
Load Diff
11
Day5/Question2/inputTest
Normal file
11
Day5/Question2/inputTest
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
70
Day5/Question2/main.cpp
Normal file
70
Day5/Question2/main.cpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
typedef uint64_t inclusiveLowerBound;
|
||||||
|
typedef uint64_t inclusiveUpperBound;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::fstream input("input");
|
||||||
|
std::string currentLine = "";
|
||||||
|
std::multimap<inclusiveLowerBound, inclusiveUpperBound> ranges{};
|
||||||
|
uint16_t positionOfHypen = 0;
|
||||||
|
uint64_t upperBound = 0, lowerBound = 0;
|
||||||
|
|
||||||
|
// Range and values are seperated by an empty \n
|
||||||
|
while (std::getline(input, currentLine, '\n')) {
|
||||||
|
if (currentLine == "") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
positionOfHypen = currentLine.find('-');
|
||||||
|
lowerBound = std::stoull(currentLine.substr(0, positionOfHypen));
|
||||||
|
upperBound = std::stoull(currentLine.substr(positionOfHypen + 1));
|
||||||
|
|
||||||
|
ranges.insert({lowerBound, upperBound});
|
||||||
|
}
|
||||||
|
|
||||||
|
// First time doing this with ranges
|
||||||
|
auto range = ranges.begin();
|
||||||
|
while (range != ranges.end()) {
|
||||||
|
auto secondRange = range;
|
||||||
|
secondRange++;
|
||||||
|
|
||||||
|
while (secondRange != ranges.end()) {
|
||||||
|
// 3 Possible cases
|
||||||
|
|
||||||
|
bool isEndOfFirstBiggerThanSecond = range->second >= secondRange->second;
|
||||||
|
// 1. Second range is fully inside First range
|
||||||
|
if (isEndOfFirstBiggerThanSecond) {
|
||||||
|
secondRange = ranges.erase(secondRange);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Second Range's end is bigger than first, but Second's start is
|
||||||
|
// smaller than first's end
|
||||||
|
if (!isEndOfFirstBiggerThanSecond &&
|
||||||
|
range->second >= secondRange->first) {
|
||||||
|
range->second = secondRange->second;
|
||||||
|
secondRange = ranges.erase(secondRange);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Nothing happens, they are not overlapping
|
||||||
|
secondRange++;
|
||||||
|
|
||||||
|
// Note: The begining of first range WILL ALWAYS be smaller than second
|
||||||
|
}
|
||||||
|
range++;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t password = 0;
|
||||||
|
for (auto keys : ranges) {
|
||||||
|
password += keys.second - keys.first + 1; // 1 is for range to be inclusive
|
||||||
|
}
|
||||||
|
std::cout << password << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user