diff --git a/day13/day13.cpp b/day13/day13.cpp new file mode 100644 index 0000000..758f1ca --- /dev/null +++ b/day13/day13.cpp @@ -0,0 +1,78 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +constexpr auto OUT_OF_SERVICE_BUS = "x"; + +std::vector readInput(const std::string &filename) { + std::vector input; + std::string line; + std::ifstream file(filename); + while (std::getline(file, line)) { + input.push_back(line); + } + + return input; +} + +std::pair> splitInput(const std::vector &input) { + int startTime = std::stoi(input.at(0)); + const std::string &rawBusses = input.at(1); + std::vector rawBusTimes; + folly::split(",", rawBusses, rawBusTimes); + + return std::pair>(startTime, rawBusTimes); +} + +int part1(int busStartTime, const std::vector &rawBusTimes) { + std::vector busTimes; + busTimes.reserve(rawBusTimes.size()); + for (const std::string &rawBusTime : rawBusTimes) { + if (rawBusTime == OUT_OF_SERVICE_BUS) { + continue; + } + + int busTime = std::stoi(rawBusTime); + busTimes.push_back(busTime); + } + + // Calculate the bus times that are immediately after the start time input + std::map nextBusses; + std::transform( + busTimes.cbegin(), busTimes.cend(), std::inserter(nextBusses, nextBusses.end()), [busStartTime](int busTime) { + // Take the ceiling when dividing + int numIntervals = (busTime + busStartTime - 1) / busTime; + int nextBusTime = busTime * numIntervals; + + return std::pair(busTime, nextBusTime); + }); + + auto minPairIterator = std::min_element( + nextBusses.cbegin(), nextBusses.cend(), [](std::pair time1, std::pair time2) { + return time1.second < time2.second; + }); + + int bestBusID = minPairIterator->first; + int bestBusTime = minPairIterator->second; + int timeToWait = bestBusTime - busStartTime; + + return bestBusID * timeToWait; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + std::cerr << argv[0] << " " << std::endl; + return 1; + } + + auto input = readInput(argv[1]); + auto parsedInput = splitInput(input); + + std::cout << part1(parsedInput.first, parsedInput.second) << std::endl; +}