#include #include #include #include // Cool thing is that you can tweak this number to find any possible digit // version of this question (so yes you can solve part 1 with this) #define JOLTAGE_MUST_LENGHT 12 int main() { std::fstream input("input"); std::string currentLine = "", subSection = ""; uint64_t totalJoltage = 0, lineLength = 0; while (std::getline(input, currentLine)) { std::string joltage = ""; lineLength = currentLine.length(); // We need to iteratively find where each digit can be at minimum and max uint16_t availableMax = lineLength - JOLTAGE_MUST_LENGHT, digitPointer = 0; for (uint16_t currentDigit = 1; currentDigit <= JOLTAGE_MUST_LENGHT; currentDigit++) { // Adjusting how much of the digits are left unparsed availableMax = lineLength - (JOLTAGE_MUST_LENGHT - currentDigit + digitPointer); // All possible digit candidates subSection = currentLine.substr(digitPointer, availableMax); char biggestDigit = '0'; uint16_t positionOfTheBiggestDigit = digitPointer; // Find the biggest MOST SIGNIFICANT digit for (char digit : subSection) { if (biggestDigit < digit) { biggestDigit = digit; positionOfTheBiggestDigit = digitPointer; } digitPointer++; } // Return to where we got the digit digitPointer = positionOfTheBiggestDigit + 1; joltage.append(1, biggestDigit); } totalJoltage += std::stoull(joltage); } std::cout << totalJoltage << std::endl; return 0; }