Files
2025-12-02 15:43:10 +02:00

41 lines
1.3 KiB
C++

#include <cmath>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::fstream file("input");
std::string currentID = "";
uint64_t rangeMin = 0, rangeMax = 0, positionOfHypen = 0,
sumOfAllInvalidIds = 0, numberOfDigits = 0;
double nthSymmetryicalNumber = 0;
while (std::getline(file, currentID, ',')) {
// I'm sure there is a better way to do this
positionOfHypen = currentID.find('-');
rangeMin = std::stoull(currentID.substr(0, positionOfHypen));
rangeMax = std::stoull(currentID.substr(positionOfHypen + 1));
for (uint64_t number = rangeMin; number <= rangeMax; number++) {
numberOfDigits = std::floor(std::log10(number)) + 1;
// Odd numbers can't satisfy the repeating pattern
if (numberOfDigits % 2 == 1) {
continue;
}
// Really only possible symmetry is here
nthSymmetryicalNumber =
number / static_cast<double>(std::pow(10, numberOfDigits >> 1) + 1);
// Checking if the number is "whole"
sumOfAllInvalidIds +=
nthSymmetryicalNumber == std::trunc(nthSymmetryicalNumber) ? number
: 0;
}
}
std::cout << sumOfAllInvalidIds << std::endl;
return 0;
}