Created
January 15, 2019 10:38
-
-
Save towc/797214891196212f202d5827807c8e06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <algorithm> | |
#include <map> | |
#include <memory> | |
struct InputLine { | |
int min; | |
std::string text; | |
}; | |
InputLine parseLine(std::string line) { | |
// 000000000011111111112222222222 | |
// 012345678901234567890123456789 | |
// [YYYY-MM-DD hh:mm] Guard #123 begins shift | |
std::string minStr = line.substr(15, 17); // mm | |
int min = std::stoi(minStr); | |
std::string text = line.substr(19); // Text | |
return InputLine{min, text}; | |
} | |
class Guard { | |
public: | |
int id; | |
std::vector<std::pair<int, int>> times; | |
int mostMin; | |
int mostVal; | |
int minsAsleep; | |
Guard (int id): id(id), mostMin(0), mostVal(0), minsAsleep(0) { } | |
void calcMins() { | |
for(int i = 0; i < 59; ++i) { | |
int sum = 0; | |
for(const std::pair<int, int>& time : this->times) { | |
if (i < time.second && i >= time.first) { | |
++sum; | |
} | |
} | |
if (sum > this->mostVal) { | |
this->mostMin = i; | |
this->mostVal = sum; | |
} | |
} | |
} | |
int getScore() { | |
return this->id * this->mostMin; | |
} | |
}; | |
int main() { | |
std::ifstream inputStream("aoc-input-4"); | |
std::string line; | |
std::vector<std::string> lines; | |
while(std::getline(inputStream, line)) { | |
lines.push_back(line); | |
} | |
std::sort(lines.begin(), lines.end()); | |
std::map<int, std::shared_ptr<Guard>> guardMap; | |
std::shared_ptr<Guard> lastGuard; | |
std::for_each(lines.begin(), lines.end(), [guardMap, lastGuard] (std::string line) mutable { | |
auto [min, text] = parseLine(line); | |
switch (text[0]) { | |
case 'G': { | |
// 0123456789 | |
// Guard #123 | |
int id = std::stoi(text.substr(7, text.find(" ", 7))); | |
lastGuard = guardMap[id]; | |
if (!lastGuard) { | |
Guard guard(id); | |
lastGuard = std::make_shared<Guard>(guard); | |
std::shared_ptr<Guard> shared = lastGuard; | |
guardMap.insert(std::pair<int, std::shared_ptr<Guard>>(id, lastGuard)); | |
} | |
std::cout << lastGuard << ", " << id << ", " << guardMap.size() << "\n"; | |
} break; | |
case 'f': { | |
lastGuard->times.push_back(std::pair<int, int>(min, 0)); | |
} break; | |
case 'w': { | |
std::pair<int, int>* span = &(lastGuard->times.back()); | |
span->second = min; | |
} break; | |
} | |
}); | |
std::vector<std::shared_ptr<Guard>> guards; | |
for(std::map<int, std::shared_ptr<Guard>>::iterator it = guardMap.begin(); it != guardMap.end(); it++) { | |
guards.push_back(it->second); | |
} | |
std::shared_ptr<Guard> mostMins = guards[0]; | |
std::shared_ptr<Guard> mostMin = guards[0]; | |
std::for_each(guards.begin(), guards.end(), [mostMins, mostMin] (std::shared_ptr<Guard> guard) mutable { | |
guard->calcMins(); | |
if (guard->minsAsleep > mostMins->minsAsleep) { | |
mostMins = guard; | |
} | |
if (guard->mostVal > mostMin->mostVal) { | |
mostMin = guard; | |
} | |
}); | |
std::cout << mostMins->getScore() << "\n" | |
<< mostMin->getScore() << "\n"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment